2013-07-26 13 views
6

Ich versuche, eine Rekordzahl historischen Orten zurückzukehrenMYSQL Select MAX Datum in einer Join-Anweisung

Was ich habe, ist:

SELECT l.location, t.transaction_id, t.date_modified 
FROM transactions as t 
INNER JOIN (
SELECT 
t1.received_id, t1.transaction_id, t1.date_modified 
FROM (
SELECT received_id, MAX(date_modified) as maxmodify 
FROM transactions 
GROUP BY received_id) as max_record 
JOIN transactions as t1 
ON (t1.received_id =max_record.received_id) 
) as whatever 
INNER JOIN locations as l 
ON l.location_id = t.location_id 
INNER JOIN received as r 
ON r.received_id = t.received_id 
WHERE t.received_id='1782' 
ORDER BY t.date_modified DESC 

Diese etwa 1 min dauert Daten zu analysieren und kehrt wie:

T-E1A 67294 2013-05-29 14:05:30 
T-E1A 67293 2013-05-29 14:05:30 
T-E1A 67294 2013-05-29 14:05:30 
T-E1A 67293 2013-05-29 14:05:30 
T-E1A 67294 2013-05-29 14:05:30 
T-E1A 67293 2013-05-29 14:05:30 
T-E1A 67294 2013-05-29 14:05:30 

Was erwarte ich wirklich zu sehen ist, Daten wie aus einer Abfrage wie folgt aus:

SELECT l.location, t.transaction_id, t.date_modified FROM transactions as t 
JOIN locations as l 
ON l.location_id = t.location_id 
JOIN received as r 
ON r.received_id = t.received_id 
WHERE t.received_id='1782' 
ORDER BY t.date_modified DESC 

Welche

T-E1A 67290 2013-05-29 13:58:26 
T-E1A 67289 2013-05-29 13:58:26 
ADJUST 67283 2013-04-26 11:33:54 
ADJUST 67284 2013-04-26 11:33:54 
ST10 67279 2013-04-26 09:52:41 
ST10 67278 2013-04-26 09:52:13 
ST10 67277 2013-04-26 09:50:58 
ST10 67276 2013-04-26 09:50:20 
SH3  67274 2013-04-26 09:49:39 

Diese zweite Abfrage gibt ist besser, aber ich möchte wirklich nur für jeden Datensatz-ID und den Ort der letzten Änderung zeigen.

Kann jemand sehen, was ich falsch mache? Ich schätze die Hilfe.

Antwort

10

So etwas ...

SELECT t1.received_id 
    , t1.transaction_id 
    , t1.date_modified 
    , l.location 
    FROM transactions t1 
    JOIN (SELECT received_id, MAX(date_modified) maxmodify FROM transactions GROUP BY received_id) max_record 
    ON max_record.received_id = t1.received_id 
    AND max_record.maxmodify = t1.date_modified 
    JOIN locations l 
    ON l.location_id = t1.location_id 
    JOIN received r 
    ON r.received_id = t1.received_id 
WHERE t1.received_id = '1782' 
ORDER 
    BY t1.date_modified DESC 

der Kern von denen dies ...

SELECT x.* 
    FROM my_table x 
    JOIN (SELECT id,MAX(thing) max_thing FROM my_table GROUP BY id) y 
    ON y.id = x.id 
    AND y.max_thing = x.thing; 
+0

Danke das ist die Route, die ich denke, ist am geeignetsten, aber ich habe Probleme, wie Sie in meiner ersten Abfrage sehen können, die versucht, die anderen Tabellen (Standorte, erhalten) auch zu bekommen. – nodsdorf

+0

Ja, Sie brauchen eine ON-Klausel mit jeder JOIN-Klausel – Strawberry

0
SELECT l.location, t.transaction_id, max(t.date_modified) FROM transactions as t 
JOIN locations as l 
ON l.location_id = t.location_id 
JOIN received as r 
ON r.received_id = t.received_id 
WHERE t.received_id='1782' 
group by l.location, t.transaction_id 
ORDER BY t.date_modified DESC 

Hilft das?

+0

I Gruppe zunächst versucht, durch, aber das Problem ist, ich brauche den max Änderungsdatum zu ergreifen und Gruppe erlaubt das nicht. Danke aber – nodsdorf