我有三张桌子。
Page Revision Text
+-----------+-------------+ +-----------+-------------+----------+ +-------------+---------+
| page_id | rev_id | | rev _id | rev_text_id | page_id | | rev_text_id | text |
+-----------+-------------+ +-----------+-------------+----------+ +-------------+---------+
| 1 | 2 | | 1 | 1 | 1 | | 1 | A |
+-------------------------- +------------------------------------+ +-----------------------+
| 2 | 5 | | 2 | 2 | 1 | | 2 | B |
+-------------------------+ +------------------------------------+ +-----------------------+
| 3 | 3 | 2 | | 3 | C |
+------------------------------------+ +-----------------------+
| 4 | 4 | 2 | | 4 | D |
+------------------------------------+ +-----------------------+
| 5 | 4 | 2 |
+------------------------------------+
我想要的输出是:
+--------+-------------+
| rev_id | rev_text_id |
+--------+-------------+
| 1 | 1 |
+----------------------+
| 3 | 3 |
+----------------------+
| 4 | |
+----------------------+
我的目标是找到可以在单个查询中从每个表中删除的rev_id和rev_text_id。由于修订表可以为多个修订保留相同的rev_text_id,因此我需要排除每页最高的rev_text_id(在第2页的示例4中)。
我开始涉足:
SELECT
r.rev_id,r.rev_text_id
FROM revision r, page p, text t
WHERE p.page_id = r.page_id
AND t.rev_text_id = r.rev_text_id
AND p.rev_id != r.rev_id
AND r.rev_text_id not in (
SELECT max(r.rev_text_id)
FROM revision AS r, page AS p
WHERE r.rev_id = p.rev_id
);
但是在我想要的输出中跳过了rev_id 4的记录,并且不包括“null case”..
答案 0 :(得分:0)
试试这个:
SELECT r.`rev_id`, r.`rev_text_id`
FROM `Revision` AS r
WHERE r.`rev_text_id` NOT IN (SELECT r.`rev_text_id`
FROM `Revision` AS r, `Page` AS p
WHERE r.`rev_id` = p.`page_latest`;)
ORDER BY r.`rev_id`;
这基本上抓住了与page_latest相等的rev_id,找到与那些rev_id相关联的rev_text_id,并使用那些rev_text_id作为过滤器。