Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

MYSQL Select MAX Date inside a join statement

I'm trying to return a record number's historic locations

What I have is:

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

This takes about 1 min to parse and returns data like:

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

What I'm really expecting to see is data like from a query like this:

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

Which returns

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

This second query is better but I really want to only show the last modified time for each record id and location.

Can anybody see what I'm doing wrong? I appreciate the help.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this...

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

the kernel of which is this...

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...