jaymc Posted July 9, 2009 Share Posted July 9, 2009 When doing a GROUP BY how is it possible to ORDER eached GROUPED results? It appears to ignore the ORDER BY and just return the first row it finds as part of the GROUP BY Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/ Share on other sites More sharing options...
celsoendo Posted July 9, 2009 Share Posted July 9, 2009 Can you post your query? Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872161 Share on other sites More sharing options...
jaymc Posted July 9, 2009 Author Share Posted July 9, 2009 SELECT t.id, t.subject, t.department, tm.date FROM fred.client_tickets t INNER JOIN fred.client_ticket_messages tm GROUP BY t.id ORDER BY tm.date ORDER BY tm.date becomes useless because of the GROUP BY Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872165 Share on other sites More sharing options...
Mchl Posted July 9, 2009 Share Posted July 9, 2009 Use MAX() or MIN() functions to select latest/earliest date. Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872176 Share on other sites More sharing options...
jaymc Posted July 9, 2009 Author Share Posted July 9, 2009 Use MAX() or MIN() functions to select latest/earliest date. That works on the individual field you apply MIN() or MAX() to but it does not pull put the entire row of the MAX(date) The other fields are still random.. Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872187 Share on other sites More sharing options...
Mchl Posted July 9, 2009 Share Posted July 9, 2009 What exactly are you trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872192 Share on other sites More sharing options...
jaymc Posted July 9, 2009 Author Share Posted July 9, 2009 I want to pull out a row from a table by its primary ID, then join another table which has multiple rows with the same ID as the primary, but not unique table 1: id-1 id-2 id-3 table 2: ticketID-1 ticketID-1 ticketID-1 ticketID-2 ticketID-3 ticketID-3 Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872230 Share on other sites More sharing options...
fenway Posted July 9, 2009 Share Posted July 9, 2009 Is this not the same as this? Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872321 Share on other sites More sharing options...
jaymc Posted July 10, 2009 Author Share Posted July 10, 2009 Hmm yes, although trying your example with new fields,database It does not work Heres what I have got so far SELECT t.id, t.subject, t.department, tm.responder, tm.date FROM fred.client_tickets t INNER JOIN ( SELECT date, responder FROM fred.client_ticket_messages GROUP BY ticketID ) AS tm USING ( date, responder ) Error: Unknown column 'date' in from clause Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872893 Share on other sites More sharing options...
Mchl Posted July 10, 2009 Share Posted July 10, 2009 Is there `date` field in both tables? Also note, that you should avoid using `date` as column name, as it has special meaning for MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/165375-group-by-order/#findComment-872930 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.