如何根据多列获取最新记录

时间:2014-11-25 09:31:42

标签: mysql phpmyadmin

我有一个类似于以下内容的表

表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

1 个答案:

答案 0 :(得分:0)

像这样......

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'