Hi all,

I trying to run a query where, after doing a UNION on two different SELECTs, I need to sort the result by username and log_date fields, and then grab the last entry for each username ('last' as determined by the ordering of the log_date field, which is a datetime).

GROUP BY seems like an obvious choice; 'GROUP BY username', to be exact. However, this seems to produce not the last row's values, but ones from a random row in the group.

I don't think the fact that I am doing this on a subquery is relevant, but just in case, I am including this info.

        Here is what the query looks like, abridged:


SELECT id,username,log_date,event_type
FROM (SELECT * FROM a
      UNION
      SELECT * from b) as h
GROUP BY username


Basically, what I need is the chronologically last event_type value for each user. I can achieve something similar by doing SELECT MAX(event_type) -- but I need the equivalent of SELECT LAST(event_type); last row instead of max-field-value row.

I keep having a feeling that I am making this way more complicated than it has to be, and that there's a very simple way to return only the last row for each username; but i am at a loss as to how to do it.


--
        Victor Danilchenko
        Senior Software Engineer, AskOnline.net
        [EMAIL PROTECTED] - 617-273-0119

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to