表错误和依赖关系如下所示:
select * from bugs;
+--------+--------+
| bug_id | status |
+--------+--------+
| 20 | NEW |
| 45 | CLOSED |
| 47 | NEW |
| 30 | NEW |
| 50 | CLOSED |
+--------+--------+
select * from dependencies;
+-----------+---------+
| dependson | blocked |
+-----------+---------+
| 20 | 45 |
| 20 | 47 |
| 30 | 50 |
+-----------+---------+
联接查询的预期结果:30
什么?从列依赖项,表依赖项中仅提取30,因为30是唯一( NEW status )阻止(col:blocked-50)状态为CLOSED的错误。 尽管dependson条目20是阻塞45,它是CLOSED但是它也阻止了47,它没有被关闭。
注意:列依赖和阻止都是bug_id的外键。
我尝试了不同的查询,但仍无法获得正确的查询...有人可以帮忙吗?
更新:仅需要状态NEW并非全部。
答案 0 :(得分:1)
尝试此查询 -
SELECT b.* FROM bugs b
JOIN (
SELECT
d.*
FROM dependencies d
JOIN bugs b
ON b.bug_id = d.blocked
GROUP BY d.dependson
HAVING COUNT(IF(status = 'CLOSED', status, NULL)) = COUNT(*)
) t
ON b.bug_id = t.dependson
答案 1 :(得分:0)
Select bugs.* From bugs Inner Join dependencies On (bugs.bug_id = dependencies.blocked) Where bugs.status = 'CLOSED' And bugs.bug_id = dependencies.blocked
实际上,您可能不需要and
声明的where
部分,但我无法对其进行测试,因此请将其放在那里以便进行测量。
修改强>
我刚看到你正在试图获得依赖而不是阻止。我会更新这个。
Select bugs.bug_id From bugs Inner Join dependencies On (bugs.bug_id = dependencies.dependedson)
Where dependencies.dependson Not In
(Select dependencies.dependson From bugs Inner Join dependencies On (bugs.bug_id = dependencies.blocked)
Where bugs.status = 'New' And bugs.bug_id = dependencies.blocked)
And bugs.bug_id = dependencies.dependson
答案 2 :(得分:0)
它没有经过测试,但我认为你不需要在这里加入。
SELECT dependson
FROM dependencies
WHERE dependson NOT IN
(SELECT bug_id FROM bugs WHERE status <> 'CLOSED')
通过这种方式,您只能获得已关闭的dependson
。