2011-12-19 26 views
8

mi hai dichiarazioni mysqlMySQL selezionare GROUP BY ordine

SELECT * 
FROM tbl_messages 
WHERE to_user_id = '$user_id' OR from_user_id = '$user_id' 
GROUP BY from_user_id 
ORDER BY date_sent DESC 

e sta producendo i risultati corretti ma non sono nell'ordine corretto.

Il raggruppamento funziona bene ma la registrazione visualizzata nel gruppo è la prima registrata immessa nel DB ma vorrei che l'ultimo record fosse visualizzato in ciascun gruppo.

C'è un modo per visualizzare l'ultimo record per ciascun gruppo?

2011-12-19 12:16:25 This is the first message 
2011-12-19 12:18:20 This is the second message 
2011-12-19 12:43:04 This is the third message 

Il gruppo mostra 'Questo è il primo messaggio' dove mi piacerebbe 'Questo è il terzo messaggio' come quello è il record/messaggio più recente.

Acclamazioni

+0

Qual è la domanda dovrebbe mostrare? –

+0

Se sono necessarie solo le due colonne (ad esempio ID e il suo timestamp più recente), questo potrebbe funzionare: http://stackoverflow.com/a/4448536/722036. È ** più veloce ** rispetto all'utilizzo di sottoquery su una tabella enorme con milioni di righe. –

risposta

9

Questo può funzionare (ma non garantita):

SELECT * 
FROM 
    (SELECT * 
    FROM tbl_messages 
    WHERE to_user_id = '$user_id' OR from_user_id = '$user_id' 
    ORDER BY date_sent DESC 
) tmp 
GROUP BY from_user_id 
ORDER BY date_sent DESC 

questo dovrebbe funzionare:

SELECT t.* 
FROM 
    tbl_messages AS t 
    JOIN 
    (SELECT from_user_id 
      , MAX(date_sent) AS max_date_sent 
     FROM tbl_messages 
     WHERE to_user_id = '$user_id' OR from_user_id = '$user_id' 
     GROUP BY from_user_id 
    ) AS tg 
    ON (tg.from_user_id, tg.max_date_sent) = (t.from_user_id, t.date_sent) 
ORDER BY t.date_sent DESC 
+0

Sembra funzionare. È necessaria una leggera modifica (aggiungi tbl_messages prima di GROUP BY) ma sembra buono. Grazie! – puks1978

+0

Grazie al salvataggio della mia giornata, a scuola ho imparato di più sugli schiavisti ... Non mi ricordo di questo grande querys ... il mio insegnante ... vecchi ricordi – delive

-1

vuoi dire qualcosa di simile:

 
SELECT * FROM tbl_messages WHERE to_user_id = '$user_id' OR from_user_id = '$user_id' GROUP BY from_user_id ORDER BY from_user_id, date_sent DESC 

2

Se i messaggi tabella ha una chiave primaria che è auto-incremento, e tutti i messaggi sono per natura più alto numero è la data più recente ... Tuttavia, poiché non lo so, vado basato sul MAX (date_sent) anziché su max (SomeIDKey), ma il principio è lo stesso.

select 
     tm2.* 
    from 
     (select tm1.from_user_id, 
       max(tm1.date_sent) LatestMsgDate 
      from tbl_messages tm1 
      group by tm1.from_user_id) MaxPerUser 

     left join tbl_messages tm2 
     on MaxPerUser.From_User_ID = tm2.From_User_ID 
     AND MaxPerUser.LatestMsgDat = tm2.Date_Sent 

    order by 
     date_sent DESC 
+0

come aggiungerei ORDER BY in MaxPerUser e GROUP BY alla fine della query. –

+1

@MohammedAbrarAhmed, nessun ordine applicabile alla query MaxPerUser interna. Tuttavia, se si desidera che i risultati esterni siano basati sulla data del messaggio più recente, è possibile ORDINARE da MaxPerUser.LatestMsgDate, se questo è il motivo per cui si desidera che i messaggi più recenti vengano spostati verso l'alto. – DRapp

8

Fare un GROUP BY dopo ORDER BY avvolgendo la tua ricerca con il GRUPPO in questo modo:

SELECT t.* FROM (SELECT * FROM table ORDER BY time DESC) t GROUP BY t.from 
+0

Anche se inserisco l'ASC nella query "t" sta prendendo DESC. –