php - how do I get month from date in mysql -
i want able fetch results mysql statement this:
select *    table   amount > 1000    but want fetch result constrained month , year (based on input user)... trying this:
select *    table   amount > 1000     , datestart = month('$m')      ...$m being month gave error. 
in table, have 2 dates:  startdate , enddate focusing on startdate. input values month , year. how phrase sql statement gets results based on month of year? 
you close - got comparison backwards (assuming startdate datetime or timestamp data type):
select *    table   amount > 1000     , month(datestart) = {$m}   caveats:
- mind using mysql_escape_string or risk sql injection attacks.
 - function calls on columns means index, if 1 exists, can not used
 
alternatives:
because using functions on columns can't use indexes, better approach use between , str_to_date functions:
where startdate between str_to_date([start_date], [format])                      , str_to_date([end_date], [format])   see documentation formatting syntax.
Comments
Post a Comment