MySQL获取每个项目的最后一条记录

标签 mysql

搞了两天了,没搞明白。希望有人能帮助我。

我有几个具有一对多关系的表。

这很难解释,所以这里是相关表的示例(文件id来自另一个表,与这个问题无关,我们在这个示例中查找的文件id是1)

Table 1 - history
+----+---------+---------+-----------+---------------------+
| id | file_id | user_id | action_id | datetime            |
+----+---------+---------+-----------+---------------------+
|  1 |       1 |       1 |         1 | 2014-02-19 19:19:49 |
|  2 |       1 |       1 |         2 | 2014-02-19 19:20:06 |
|  3 |       1 |       1 |         3 | 2014-02-19 19:32:09 |
|  4 |       2 |       2 |         1 | 2014-02-20 11:52:59 |
|  5 |       2 |       2 |         2 | 2014-02-20 11:53:08 |
+----+---------+---------+-----------+---------------------+

Table 2 - file_items
+----+---------+------------+---------------+
| id | file_id | item_id    | remark        |
+----+---------+------------+---------------+
|  1 |       1 |          1 | item1 remark  |
|  2 |       1 |         20 | item2 remarks |
|  3 |       2 |          2 | test          |
+----+---------+------------+---------------+

Table 3 - item_statuses
+----+----------------+--------------+----------------+----------------------+
| id | file_action_id | file_item_id | item_status_id | comment              |
+----+----------------+--------------+----------------+----------------------+
|  1 |              2 |            1 |              1 |                      |
|  2 |              2 |            2 |              1 |                      |
|  3 |              3 |            1 |              2 | status comment       |
|  4 |              3 |            2 |              1 | item2 status comment | 
|  5 |              5 |            3 |              1 |                      |
+----+----------------+--------------+----------------+----------------------+

t1.action_idt3.file_action_id 相关

t2.item_idt3.file_item_id

相关

t1.file_idt2.file_id 相关

我正在尝试获取特定文件的每个项目的最新状态(本例中为文件 ID 1)。

期望的结果:

+----------------+-----------------+------------+---------------------+----------------+----------------------+
| file_action_id | file_item_id | remark        | file_item_status_id | item_status_id | COMMENT              |
+----------------+-----------------+------------+---------------------+----------- ----+----------------------+
|              3 |            1 | item1 remark  |                   3 |              2 | status comment       |
|              3 |            2 | item2 remarks |                   4 |              1 | item2 status comment |
+----------------+--------------+---------------+---------------------+----------------+----------------------+

我得到了什么:

+----------------+-----------------+------------+---------------------+----------------+----------------------+
| file_action_id | file_item_id | remark        | file_item_status_id | item_status_id | COMMENT              |
+----------------+-----------------+------------+---------------------+----------  -----+---------------------+
|              3 |            1 | item1 remark  |                   1 |              1 |                      |
|              3 |            1 | item1 remark  |                   3 |              2 | status comment       |
|              3 |            2 | item2 remarks |                   2 |              1 |                      |
|              3 |            2 | item2 remarks |                   4 |              1 | item2 status comment |
+----------------+--------------+---------------+---------------------+----------------+----------------------+

查询:

SELECT
t1.id AS file_action_id,
t2.id AS file_item_id,
t2.remark AS remark,
t5.id AS file_item_status_id,
t5.item_status_id AS item_status_id,
t5.comment AS COMMENT

FROM `file_history` AS t1

LEFT JOIN `file_items` AS t2
ON (t1.file_id = t2.file_id)

LEFT JOIN `file_item_statuses` AS t5
ON (t2.id = t5.file_item_id)

WHERE t1.file_id = 1
AND
t1.id = (SELECT MAX(id) FROM `file_history` WHERE file_id = 1)

我尝试使用 GROUP BYORDER BY 但它不适合我。

SQL FIDDLE HERE

最佳答案

好的,所以您想要特定文件的每个项目的最新状态,因此,我要做的就是使用仅具有最新状态的表进行INNER JOIN

为此,我将使用具有正确数据的子查询来LEFT JOIN(子查询将只获取具有最大状态 ID 值的行,我检查了这一点以实现它:SQL Select only rows with Max Value on a Column )

SELECT fs.id          
     , fs.file_item_id
     , fs.item_status_id
     , fs.comment  
FROM file_item_statuses fs
INNER JOIN (
    SELECT MAX(id) AS id
    FROM file_item_statuses
    GROUP BY file_item_id
    ) maxfs 
ON maxfs.id = fs.id

这个子查询,你必须将它与主查询左连接:

SELECT
t1.id AS file_action_id,
t2.id AS file_item_id,
t2.remark AS remark,
t5.id AS file_item_status_id,
t5.item_status_id AS item_status_id,
t5.comment AS COMMENT

FROM `file_history` AS t1

LEFT JOIN `file_items` AS t2
ON (t1.file_id = t2.file_id)

INNER JOIN ( 
    SELECT fs.id          
         , fs.file_item_id
         , fs.item_status_id
         , fs.comment  
    FROM file_item_statuses fs
    INNER JOIN (
        SELECT MAX(id) AS id
        FROM file_item_statuses
        GROUP BY file_item_id
    ) maxfs 
    ON maxfs.id = fs.id ) AS t5
ON (t2.id = t5.file_item_id)

WHERE t1.id = (SELECT MAX(id) FROM `file_history` WHERE file_id = 1) 

GROUP BY file_item_id

我运行它并给出了你想要的结果,享受吧!

关于MySQL获取每个项目的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21901190/

相关文章:

mysql过滤限制a,b

mysql - 连接到 MySQL 问题

mysql - 查询无法在多列的 case 语句后添加 where 子句

php - mysql-php表单提交重定向到同一页面而不输入数据

php - MySQL 日期时间字段和夏令时——如何引用 "extra"小时?

mysql - 如何在 mysql 中实现该 SQL 查询?

mysql - SQL 查询选择除 'ignore_table' 中定义的记录之外的整个表

MySql If Then 在 Select 语句中

mysql - 我正在将大量记录插入到一​​个大表中,我应该删除索引直到完成吗?

php - MySQL 查询帮助 - 最后已知的测试日期和当前测试日期