我已經在它兩天了,無法弄清楚。希望可以有人幫幫我。MySQL得到每個項目的最後一個記錄
我有幾個表與一對多的關係。
這是很難解釋的,所以這裏是有關表的例子(文件編號來自另一個表中,並沒有涉及到這個問題,我們在這個例子中尋找文件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_id
有關t3.file_action_id
t2.item_id
有關t3.file_item_id
t1.file_id
有關t2.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 BY
和ORDER BY
但它並沒有爲我做的。
感謝,偉大工程!另一個答案也適用。我沒有太多數據來測試效率。我希望將更有效率的標記作爲兩者都有效的答案。 –
知道它的唯一方法:在插入數百個虛擬數據之後,在您的機器上運行這兩個數據並檢查速度。無論如何,在子查詢中,您可以更改我提供的方式(答案中的第一個選項),並使用我檢查過的答案中的第二個選項來實現它。這樣你以後可以改進它。啊!不要忘記包含適當的索引! (使用您用於連接的字段構建它們) – Chococroc