简体   繁体   中英

How to fetch the information from latest record in MySQL (MardiaDB) dependent table?

I have a MySQL database (also working as MariaDB on some servers) with 2 main tables. One is the tickets and the another is ticket_contents as below:

Tickets
+----+---------+-------------+
|id  + subject | Create date |
+----+---------+-------------+
|1   | Ticket1 | 2020-05-24  |
+----+---------+-------------+
|2   | Ticket2 | 2020-05-25  |
+----+---------+-------------+
Ticket Contents
+----+-----------+---------------------+-------------+
|id  + ticket_id | update date_time    | content     |
+----+-----------+---------------------+-------------+
|1   | 1         | 2020-05-24 08:30:15 | msg1        |
+----+-----------+---------------------+-------------+
|1   | 2         | 2020-05-25 10:05:15 | msg2        |
+----+-----------+---------------------+-------------+
|1   | 1         | 2020-05-25 12:15:00 | msg3        |
+----+-----------+---------------------+-------------+

What I need is to have the list of tickets with their latest content record time as this and with the latest update order for whole tickets:

+----------+----------+---------------------+
|ticket_id |subject   |last_update          |
+----------+---_------+---------------------+
|1         | Ticket1  | 2020-05-25 12:15:00 |
+----------+----------+---------------------+
|2         | Ticket2  | 2020-05-25 10:05:15 |
+----------+----------+---------------------+

This is the code I have written, but it does not provide the correct latest child record information.

SELECT t.id, c.date_time FROM tickets t JOIN 
   (SELECT ticket_id, date_time FROM tickets_contents GROUP BY ticket_id) c on t.id = c.ticket_id 
group by t.id order by date_time desc 

Can you help me to correct my SQL code please?

You had it almost right, you must use MAX to get the highest date of a GROUP BY

SELECT t.id,t.subject,t2.maxtime FROM Tickets t INNER JOIN (SELECT MAX(`update date_time`) maxtime,ticket_id FROM `Ticket Contents` GROUP BY ticket_id) t2 ON t.id = t2.ticket_id
 id | subject | maxtime -: |:------ |:------------------ 1 | Ticket1 | 2020-05-25 12:15:00 2 |  Ticket2 | 2020-05-25 10:05:15 

db<>fiddle here

Provided that your DBMS version is 10.2+, then window functions such as RANK() might be used to determine the tickets with latest update:

SELECT tt.id AS ticket_id, tt.subject, tt.date_time AS last_update
  FROM
  (
   SELECT t.*, tc.date_time,
          RANK() OVER (PARTITION BY tc.ticket_id ORDER BY tc.date_time DESC ) AS rnk
     FROM tickets t
     JOIN tickets_contents tc
       ON tc.ticket_id = t.id
  ) tt    
  WHERE rnk = 1
  ORDER BY ticket_id;

Demo

where PARTITION BY stands for grouping for each column concerned (in this case: ticket_id ), and ORDER BY... DESC is used to filter the last record (in this case: latest ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM