sql >> Database >  >> RDS >> Mysql

MYSQL Selecteer MAX Date in een join-instructie

Zoiets...

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

waarvan de kern dit is...

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;


  1. VLDB's in de 20-tieners:je hebt een grotere nodig ...

  2. Wat is de database, waarom database?

  3. Een database importeren via de opdrachtregel

  4. Hoe rangschikkingsposities aan rijen toe te voegen met DENSE_RANK() in SQL