Reputation: 1653
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).
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!
Upvotes: 0
Views: 1680
Reputation: 265341
You are not including stat2.build_id
in your WHERE clause (and I think your ON
columns taken from the wrong tables):
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
Upvotes: 2
Reputation: 183381
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.)
Edited to add: Here is a MySQL transcript demonstrating the above:
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:
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
;
Upvotes: 2
Reputation: 52117
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
.Upvotes: 0
Reputation: 145
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
?
Upvotes: 0