at0mic Posted September 6, 2009 Share Posted September 6, 2009 With reference to the tizag article "MySQL GROUP BY - Aggregate Functions" here http://www.tizag.com/mysqlTutorial/mysqlgroupby.php I've got something similar with an additional date collumn. I want to show one of each type where the date is most recent (but not past). I've tried the following but it doesn't work: $today = getdate(); $query = "SELECT type, date FROM products WHERE date > '$today' GROUP BY type"; I could do a seperate query for each type but it would be nice if there's a way to do it with one query. Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/ Share on other sites More sharing options...
cbolson Posted September 6, 2009 Share Posted September 6, 2009 Hi, You could try something like this: $query = "SELECT DISTINCT(type), MAX(date) FROM products WHERE date > curdate() ORDER BY date DESC"; Not 100% sure about it but worth a try Chris Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-913716 Share on other sites More sharing options...
at0mic Posted September 6, 2009 Author Share Posted September 6, 2009 Thanks for your post but your code gave the error: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause I added the GROUP BY clause but some of the dates were in the past. Any other ideas? Here's the actual code I used $query = "SELECT DISTINCT(pf_type), MAX(pf_date), pf_date FROM paignton_fixtures WHERE pf_date > curdate() GROUP BY pf_type ORDER BY pf_date DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-913743 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2009 Share Posted September 6, 2009 If you would post some of your data and the results you are getting it would help. Specifically show what you are getting for pf_date and what the expected value should be for any group of pf_type. Also, DISTINCT is not a function that applies to column names. It removes duplicate ROWS in the result set. Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-913755 Share on other sites More sharing options...
at0mic Posted September 7, 2009 Author Share Posted September 7, 2009 Here is the code I'm using: $query = "SELECT pf_type, MIN(pf_date) FROM paignton_fixtures WHERE pf_date > curdate() GROUP BY pf_type"; $result = mysql_query($query) or die(mysql_error()); while( $row = mysql_fetch_array( $result ) ) { $pf_date = date('d M y', $row['MIN(pf_date)']); echo "<tr><td>{$row['pf_type']}</td><td>$pf_date</td></tr>"; } This retrieves the following from MySQL TypeDate Colts22 Aug 09 Development22 Aug 09 First08 Aug 09 Ladies03 Oct 09 Vets12 Sep 09 The pf_type part is working ok because it correctly retrieves one of each. However the date part isn't working because 08 Aug 09 is in the past. I want it to retrieve the next dates in the future for each type. Not an apparently random date. Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-914259 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2009 Share Posted September 7, 2009 What does pf_date look like in your database table? Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-914264 Share on other sites More sharing options...
at0mic Posted September 7, 2009 Author Share Posted September 7, 2009 pf_date is the number of seconds that have passed since Jan 1 1970 Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-914352 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2009 Share Posted September 7, 2009 Comparing a Unix Timestamp with curdate() (or the getdate() value that was in your first post) is meaningless because they are not in the same units/format. Do the values include an actual time in any day or are they at midnight of a day with zero seconds for the time? Also, what do you want to compare them with? The current Unix Timestamp (with whatever the current number of seconds in the day is) or do you want to compare them with a Unix Timestamp that represents the start of the current day? You would be better off using a mysql DATE yyyy-mm-dd or DATETIME yyyy-mm-dd hh:mm:ss data type (depending of if you need the time.) Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-914355 Share on other sites More sharing options...
at0mic Posted September 8, 2009 Author Share Posted September 8, 2009 Of course yes you're right. I just changed it to time() and it worked perfectly. So thanks cbolson for your query and thanks PFMaBiSmAd for the date info. Quote Link to comment https://forums.phpfreaks.com/topic/173336-solved-mysql-group-by/#findComment-914536 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.