簡體   English   中英

如何獲取基於多列的最新記錄

[英]How to get the latest records based on multiple columns

我有一張桌子,看起來像下面

表格1

+----+------+-----------+
| ID | Name | Status    |
+----+------+-----------+
| 1  | x    | Active    |
| 2  | y    | Active    |
| 3  | z    | InActive  |
+----+------+-----------+

表2

+----+-------------+--------+
| ID | Table1_Name |  Name2 |
+----+-------------+--------+
| 1  |  x          |  x1    |
| 2  |  x          |  x2    |
| 3  |  y          |  y1    |
| 4  |  y          |  y2    |
+----+-------------+--------+

表3

+----+--------------+---------------+-------------------------+---------+
| ID |  Table1_Name |  Table2_Name2 |       Timestamp         | user_id |
+----+--------------+---------------+-------------------------+---------+
| 1  |    x         |    x1         |    2014-11-24 18:56:34  |   28    |
| 2  |    x         |    x2         |    2014-11-24 18:56:59  |   28    |
| 3  |    y         |    y1         |    2014-11-24 18:56:45  |   28    |
| 4  |    y         |    y2         |    2014-11-24 18:56:40  |   28    |
| 5  |    y         |    y2         |    2014-11-24 17:56:45  |   28    |
| 6  |    x         |    x2         |    2014-11-24 17:56:58  |   28    |
| 7  |    x         |    x1         |    2014-11-24 17:56:20  |   28    |
| 8  |    y         |    y1         |    2014-11-24 17:56:36  |   28    |
| 9  |    y         |    y2         |    2014-11-24 17:56:15  |   28    |
+----+--------------+---------------+-------------------------+---------+

現在,我正在嘗試編寫查詢以顯示以下輸出。

 +----+-------------+--------------+----------------------+---------+
 | ID | Table1_Name | Table2_Name2 |       Timestamp      | user_id |
 +----+-------------+--------------+----------------------+---------+
 | 1  |    x        |     x1       |  2014-11-24 18:56:34 |    28   |
 | 2  |    x        |     x2       |  2014-11-24 18:56:59 |    28   |
 | 3  |    y        |     y1       |  2014-11-24 18:56:45 |    28   |
 | 4  |    y        |     y2       |  2014-11-24 18:56:40 |    28   |
 +----+-------------+--------------+----------------------+---------+

這些是根據當前樣本表得出的最新記錄
我嘗試了以下查詢,但無法成功獲得正確的結果。 誰能建議我如何獲得正確的結果。

SELECT table3.`table1_name`, table3.`user_id`,    
    table3.`table2_Name2`,table1.`table1_name`,
    MAX(table3.`Timestamp`) as latest_Timestamp  
FROM `test_table3` table3, `test_table1` table1  
where table3.`user_id`='28' 
    AND   table1.`status` = 'Active' 
    AND table3.`table1_name` = table1.`table1_name`  
GROUP BY table3.`exercise_id` 
ORDER BY table3.`quality_id`, table3.`Timestamp` desc

像這樣

SELECT t3.* FROM
Table3 t3
INNER JOIN Table1 t1 ON t1.Table1_Name = t3.Table1_name
INNER JOIN -- join on a subquery to get the latest time. Put all the columns you need in the group by and select...
    (SELECT Table1_Name, Table2_Name2, user_id,max(Timestamp) as max_timestamp
    FROM Table2 
    GROUP BY Table1_Name, Table2_Name2, user_id) AS maxtimes
-- joining on all the columns and the maximum time
ON t3.Table1_Name = maxtimes.Table1_Name 
    AND t3.Table2_Name2 = maxtimes.Table2_Name2 
    AND t3.user_id = maxtimes.user_id
    AND t3.Timestamp = maxtimes.max_timestamp
WHERE t1.Status = 'Active' and t3.user_id = '28'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM