0
我有一個表看起來像這樣MySQL查詢模擬版本號
+-----------+---------+-------+------------------------+-----------------------+
| commentID | reftype | refID | comment | timestamp |
+-----------+---------+-------+------------------------+-----------------------+
| 1 | A | 123 | Comment 1 | 2015-11-19 09:05:18 |
| 1 | A | 123 | Comment 1, First edit | 2015-11-19 09:06:18 |
| 2 | A | 123 | Comment 2 | 2015-11-19 09:05:44 |
| 1 | A | 123 | Comment 1, Second edit | 2015-11-19 10:05:23 |
+-----------+---------+-------+------------------------+-----------------------+
我的目標是選擇每個評論的最新編輯和添加一個版本號列。所以,我希望的結果是這樣的
+-----------+---------+-------+------------------------+---------------------+-----+
| commentID | reftype | refID | comment | timestamp | rev |
+-----------+---------+-------+------------------------+---------------------+-----+
| 1 | A | 123 | Comment 1, Second edit | 2015-11-19 10:05:23 | 3 |
| 2 | A | 123 | Comment 2 | 2015-11-19 09:05:44 | 1 |
+-----------+---------+-------+------------------------+---------------------+-----+
使用下面的查詢我得到相當接近,但我怎麼得到我的修訂列?我的方法可能嗎?還是需要完全改變它?
SELECT c1.*
FROM comments c1
LEFT JOIN comments c2 ON (c1.commentID = c2.commentID AND c1.timestamp < c2.timestamp)
WHERE c2.timestamp IS NULL
AND c1.referencetype = 'A'
AND c1.referencekey = 123
ORDER BY timestamp DESC
上述查詢給我
+-----------+---------+-------+------------------------+---------------------+
| commentID | reftype | refID | comment | timestamp |
+-----------+---------+-------+------------------------+---------------------+
| 1 | A | 123 | Comment 1, Second edit | 2015-11-19 10:05:23 |
| 2 | A | 123 | Comment 2 | 2015-11-19 09:05:44 |
+-----------+---------+-------+------------------------+---------------------+
謝謝,但我不沒辦法。我如何從此獲取修訂號碼? – daker
我編輯過,請檢查.... –