Jump to content

[SOLVED] MySQL GROUP BY


at0mic

Recommended Posts

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.

Link to comment
Share on other sites

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"; 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.