dmhall0 Posted March 15, 2012 Share Posted March 15, 2012 I need to pull data from a MySQL table that has a datetime field. The user selects 2 dates, a start and stop, and I need to filter the data by everything between and including those dates. The LIKE doesn't seem to work with a comparison operator. Here is what I have. $start_date = '2012-01-01'; $end_date = '2012-02-28'; $query = "SELECT ship_date, sku, shipqty, fgt_cost FROM shipments WHERE ship_date >= LIKE '$start_date%' AND ship_date <= LIKE '$end_date%'"; How can I make this work? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/259005-comparison-operator-with-like-possible/ Share on other sites More sharing options...
thomasw_lrd Posted March 15, 2012 Share Posted March 15, 2012 Like is a comparison operator. I have a field called last_name with the following data Robert Johnson Williams When I do Select * from table where last_name like 'Will%'; I get Williams You can't use both. Quote Link to comment https://forums.phpfreaks.com/topic/259005-comparison-operator-with-like-possible/#findComment-1327802 Share on other sites More sharing options...
mikosiko Posted March 15, 2012 Share Posted March 15, 2012 drop the LIKE and anything related to it... just format your datetime field date() ; date_format().... you pick http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date Quote Link to comment https://forums.phpfreaks.com/topic/259005-comparison-operator-with-like-possible/#findComment-1327805 Share on other sites More sharing options...
smerny Posted March 15, 2012 Share Posted March 15, 2012 while using date() you'll probably want to add 1 day to your end_date, since you said "and including those dates." date("Y-m-d", strtotime($end_date)) . " +1 day") Quote Link to comment https://forums.phpfreaks.com/topic/259005-comparison-operator-with-like-possible/#findComment-1327814 Share on other sites More sharing options...
dmhall0 Posted March 15, 2012 Author Share Posted March 15, 2012 Thanks guys. I used ... WHERE DATE(ship_date) <= '$first_date' ... and that worked. Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/259005-comparison-operator-with-like-possible/#findComment-1327815 Share on other sites More sharing options...
fenway Posted March 17, 2012 Share Posted March 17, 2012 FYI, that would negate the use of an index -- you might want to add a "fake" time to the RVALUE instead. Quote Link to comment https://forums.phpfreaks.com/topic/259005-comparison-operator-with-like-possible/#findComment-1328414 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.