我有两张桌子Documents
和Revisions
文件台
+--------+--------------------+
| DataID | Description |
+--------+--------------------+
| 1 | This is document 1 |
| 2 | This is document 2 |
| 3 | This is document 3 |
| 4 | This is document 4 |
| 5 | This is document 5 |
+--------+--------------------+
修订表
+--------+---------+-----------+
| DataID | Version | State |
+--------+---------+-----------+
| 1 | 1 | Published |
| 2 | 1 | Published |
| 3 | 1 | ? |
| 3 | 2 | Published |
| 4 | 1 | Published |
+--------+---------+-----------+
所需输出
+--------+--------------------+---------+
| DataID | Description | Version |
+--------+--------------------+---------+
| 1 | This is document 1 | 1 |
| 2 | This is document 2 | 1 |
| 3 | This is document 3 | 2 |
| 4 | This is document 4 | 1 |
| 5 | This is document 5 | N/A |
+--------+--------------------+---------+
我的查询:
SELECT d.*, h.version
FROM documents d
LEFT revisions h
ON h.dataID=d.dataID
LEFT JOIN (
SELECT dataID, MAX(version) as version
FROM revisions
GROUP BY dataID
) dh
ON dh.dataID=h.dataID
AND dh.version=h.version
AND h.state='Published'
这个左连接的问题它也会列出其中version 1
的docid 3
,state ?
而我只想要发布的版本(如果有的话),否则,只需列出版本为 NULL 的列表文档。
编辑:请注意,如果它找到条目而不仅仅是版本号
,我还想要数据中的所有列数据
有人可以帮忙吗?revision