i have a table like this:
name date time
tom | 2011-07-04 | 01:09:52
tom | 2011-07-04 | 01:09:52
mad | 2011-07-04 | 02:10:53
mad | 2009-06-03 | 00:01:01
i want oldest name first:
SELECT *
ORDER BY date ASC, time ASC
GROUP BY name
(->doesn't work!)
now it should give me first mad(has earlier date) then tom
but with GROUP BY name ORDER BY date ASC, time ASC
gives me the newer mad first because it groups before it sorts!
again: the problem is that i can't sort by date and time before i group because GROUP BY must be before ORDER BY!
I think this is what you are seeking :
For the time, you have to make a mysql date via STR_TO_DATE :
So :
This is not the exact answer, but this might be helpful for the people looking to solve some problem with the approach of ordering row before group by in mysql.
I came to this thread, when I wanted to find the latest row(which is
order by date desc
but get the only one result for a particular column type, which isgroup by column name
).One other approach to solve such problem is to make use of aggregation.
So, we can let the query run as usual, which sorted asc and introduce new field as
max(doc) as latest_doc
, which will give the latest date, with grouped by the same column.Suppose, you want to find the data of a particular column now and max aggregation cannot be done. In general, to finding the data of a particular column, you can make use of
GROUP_CONCAT aggregator
, with some unique separator which can't be present in that column, likeGROUP_CONCAT(string SEPARATOR ' ') as new_column
, and while you're accessing it, you can split/explode the new_column field.Again, this might not sound to everyone. I did it, and liked it as well because I had written few functions and I couldn't run subqueries. I am working on codeigniter framework for php.
Not sure of the complexity as well, may be someone can put some light on that.
Regards :)
Another way to solve this would be with a
LEFT JOIN
, which could be more efficient. I'll first start with an example that considers only the date field, as probably it is more common to store date + time in one datetime column, and I also want to keep the query simple so it's easier to understand.So, with this particular example, if you want to show the oldest record based on the date column, and assuming that your table name is called
people
you can use the following query:What the
LEFT JOIN
does, is when thep.date
column is at its minimum value, there will be nop2.date
with a smaller value on the left join and therefore the correspondingp2.date
will beNULL
. So, by addingWHERE p2.date is NULL
, we make sure to show only the records with the oldest date.And similarly, if you want to show the newest record instead, you can just change the comparison operator in the
LEFT JOIN
:Now, for this particular example where date+time are separate columns, you would need to add them in some way if you want to query based on the datetime of two columns combined, for example:
You can read more about this (and also see some other ways to accomplish this) on the The Rows Holding the Group-wise Maximum of a Certain Column page.
I had a different variation on this question where I only had a single
DATETIME
field and needed alimit
after agroup by
ordistinct
after sorting descending based on thedatetime
field, but this is what helped me:In this instance with the split fields, if you can sort on a concat, then you might be able to get away with something like:
Then if you wanted to limit you would simply add your limit statement to the end:
Use a subselect:
Another method:
GROUP BY groups on the first matching result it hits. If that first matching hit happens to be the one you want then everything should work as expected.
I prefer this method as the subquery makes logical sense rather than peppering it with other conditions.