pacchiee Posted July 8, 2009 Share Posted July 8, 2009 Hello, I have a table with three columns - id, product, date. id is unique + auto increment. Entries of many products go into the same table along with date. Now, I need to fetch the latest entry from this table for a given product based on date, which am not able to do. Please help. Thanks in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/165210-fetching-latest-entry-from-mysql-database/ Share on other sites More sharing options...
rhodesa Posted July 8, 2009 Share Posted July 8, 2009 first...i would highly recommend not using 'date' as a column name since it is a reserved word in MySQL. but, the query should be: SELECT * FROM tableName WHERE product = 'foobar' ORDER BY `date` DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/165210-fetching-latest-entry-from-mysql-database/#findComment-871175 Share on other sites More sharing options...
PFMaBiSmAd Posted July 8, 2009 Share Posted July 8, 2009 And what format is your date? Quote Link to comment https://forums.phpfreaks.com/topic/165210-fetching-latest-entry-from-mysql-database/#findComment-871178 Share on other sites More sharing options...
pacchiee Posted July 8, 2009 Author Share Posted July 8, 2009 first...i would highly recommend not using 'date' as a column name since it is a reserved word in MySQL. Oh! Okay, I will change it. And what format is your date? It is in YYYY-MM-DD format. Quote Link to comment https://forums.phpfreaks.com/topic/165210-fetching-latest-entry-from-mysql-database/#findComment-871219 Share on other sites More sharing options...
pacchiee Posted July 8, 2009 Author Share Posted July 8, 2009 Further, how to select data from this table by matching only date or month or year? For instance, lets assume $today = date('Y-m-d'); SELECT * FROM tableName WHERE product = 'foobar' AND MONTH(date) = MONTH($today) Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/165210-fetching-latest-entry-from-mysql-database/#findComment-871593 Share on other sites More sharing options...
fenway Posted July 15, 2009 Share Posted July 15, 2009 Looks fine to me Quote Link to comment https://forums.phpfreaks.com/topic/165210-fetching-latest-entry-from-mysql-database/#findComment-875984 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.