[英]SQL query to get expected result
这是我的数据库架构
Table_A
-id
-status
Table_B
-id
Table_A_has_B
-id
-id_A
-id_B
我需要从Table_B中选择所有记录,其中所有关联的Table_A记录的status=1
均为status=1
,如果Table_B记录没有任何关联的Table_A,则也应选择。
测试用例:
CREATE TABLE table_a (id int(2),status int(1));
INSERT INTO table_a (id, status)
VALUES (1,1),(2,0),(3,1),(4,1),(5,1);
CREATE TABLE table_b (id int(2));
INSERT INTO table_b (id) VALUES (1),(2),(3),(4);
CREATE TABLE table_a_has_b (id int(2),id_A int(2),id_B int(2));
INSERT INTO table_a_has_b (id, id_A, id_B)
VALUES(1, 1, 1),(2, 2, 1),(3, 3, 1),(4, 4, 2),(5, 5, 2),(6, 3, 4),(7, 4, 4);
查询应选择:
+----+
|b.id|
+----+
| 2|
| 3|
| 4|
+----+
编辑答案-
select b.* from table_B b
left outer join table_A_has_B ab on ab.id_B = b.id
where ab.id in (
select id from table_A_has_B ab
where (id_A in (select id from table_A where status = 1 ))
)
or b.id not in
(select id_B from table_A_Has_B )
旧答案-
select b.* from table_B b
left outer join table_A a on b.id = a.id
where a.status = 1 or a.id is null
Select ...
From Table_B As B
Left Join (
Select AB.id_B, A.id, A.status
From Table_A_has_B As AB
Join Table_A As A
On A.id = AB.id_A
Where A.status = 1
) As Z
On Z.id_B = B.id
原始帖子的这一部分内容不清楚: if a Table_B record hasn't any associated Table_A also should be selected.
。
如果只需要来自Table_B的行,则:
Select B.*
From Table_B As B
Where Exists (
Select 1
From Table_A_has_B As AB
Join Table_A As A
On A.id = AB.id_A
Where A.status = 1
And AB.id_B = B.id
)
如果要从Table_ B
行,或者给定id_B
Table_A_has_B
中没有行,或者必须将行与status
为1的Table_A项目相关联,则:
Select B.*
From Table_B As B
Where Exists (
Select 1
From Table_A_has_B As AB
Join Table_A As A
On A.id = AB.id_A
Where A.status = 1
And AB.id_B = B.id
)
Or Not Exists (
Select 1
From Table_A_has_B As AB
Where AB.id_B = B.id
)
如果您要的是Table_A中的行,但仅状态为1的行,而对于所有其他行,则为空,那么我提供的第一个查询就是解决方案。 为我们提供一些预期的输出结果显然会极大地帮助您。
编辑给定的OP更新
有了您的更新(现在只能使您清楚地知道要实现的目标),您可以执行以下操作:
Select B.id
From Table_B As B
Where Not Exists (
Select 1
From Table_A_has_B As AB
Join Table_A As A
On A.id = AB.id_A
Where A.status <> 1
And AB.id_B = B.id
)
通过改写您的需求语句以选择不具有状态<> 1关联的table_A的table_B,我进入以下查询语句:
select distinct b.*
from table_B b
left outer join table_A_has_B ab on ab.id_B = b.id
left outer join table_A a on a.id = ab.id_A and a.status <> 1
where a.id is null
要么
select b.*
from table_B b
where not exists (select null from table_A_has_B ab
inner join table_A a on a.id = ab.id_A and a.status <> 1
where ab.id_B = b.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.