[英]sql self join on multiple attributes
I have a (legacy) table that has columns in it: 我有一个(旧式)表,其中带有列:
bug_num build_id closed_to
1 3 NULL
2 4 NULL
3 NULL 1
4 3 NULL
5 NULL 2
I want to write a query where it will select all bugs from a specific build, and all bugs that were closed to a bug in that build. 我想编写一个查询,从中选择特定版本中的所有错误,以及该版本中与错误无关的所有错误。 So, if I wanted to do it for build 3, it would include #s 1 and 4 (since they're in build 3) and also 3, since it was closed to a bug in build 3 (1). 因此,如果我想在构建3中执行此操作,它将包括#1和4(因为它们在构建3中)和3,因为它已对构建3(1)中的错误关闭。
I thought I was close with: 我以为我很喜欢:
SELECT stat.bug_num,
stat.build_id
FROM bug_status stat
JOIN bug_status stat2
ON stat2.closed_to = stat.bug_num
WHERE stat.build_id = 3;
...but it doesn't seem to be giving me the desired result. ...但是似乎并没有给我想要的结果。 Thanks for your help! 谢谢你的帮助!
You are not including stat2.build_id
in your WHERE clause (and I think your ON
columns taken from the wrong tables): 您没有在WHERE子句中包含stat2.build_id
(我认为您的ON
列取自错误的表):
SELECT stat.bug_num, stat.build_id
FROM bug_status stat
LEFT JOIN bug_status stat2
ON stat.closed_to = stat2.bug_num
WHERE stat.build_id = 3 OR stat2.build_id = 3
SELECT stat.bug_num,
stat.build_id
FROM bug_status stat
WHERE stat.build_id = 3
OR stat.closed_to IN
( SELECT stat2.bug_num
FROM bug_status stat2
WHERE stat2.build_id = 3
)
;
(It's also possible to do this with a JOIN, or with a JOIN and a UNION, but I believe the above is the most intuitive way.) (也可以使用JOIN或JOIN和UNION来执行此操作,但是我相信以上是最直观的方法。)
Edited to add: Here is a MySQL transcript demonstrating the above: 编辑添加:这是一个MySQL成绩单,展示了上述内容:
mysql> create table bug_status
-> (bug_num numeric, build_id numeric, closed_to numeric);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into bug_status values (1, 3, null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into bug_status values (2, 4, null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into bug_status values (3, null, 1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into bug_status values (4, 3, null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into bug_status values (5, null, 2);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT stat.bug_num,
-> stat.build_id
-> FROM bug_status stat
-> WHERE stat.build_id = 3
-> OR stat.closed_to IN
-> ( SELECT stat2.bug_num
-> FROM bug_status stat2
-> WHERE stat2.build_id = 3
-> )
-> ;
+---------+----------+
| bug_num | build_id |
+---------+----------+
| 1 | 3 |
| 3 | NULL |
| 4 | 3 |
+---------+----------+
3 rows in set (0.00 sec)
Edited to add , since the IN (...)
approach doesn't seem to work in the OP's version of MySQL: Here is an alternative query that gives the same result: 编辑为add ,因为IN (...)
方法在OP的MySQL版本中似乎不起作用:这是一个提供相同结果的替代查询:
SELECT stat.bug_num,
stat.build_id
FROM bug_status stat
LEFT
OUTER
JOIN bug_status stat2
ON stat.closed_to = stat2.bug_num
WHERE stat.build_id = 3
OR stat2.build_id = 3
;
Why not: 为什么不:
DECLARE @build_id int = <the build id>
SELECT stat.bug_num, stat.build_id, stat.closed_to
FROM bug_status stat
WHERE stat.build_id = @build_id
OR stat.closed_to = @build_id
? ?
This query... 这个查询...
SELECT *
FROM bug_status t1
WHERE
build_id = 3
OR EXISTS (
SELECT *
FROM bug_status t2
WHERE
t2.build_id = 3
AND t1.closed_to = t2.bug_num
)
...produces the following result: ...产生以下结果:
bug_num build_id closed_to
1 3 NULL
3 NULL 1
4 3 NULL
In plain English: select the rows such that: 用简单的英语:选择以下行:
build_id = 3
build_id = 3
. 还是有一个build_id = 3
的相关行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.