[英]mysql how to select items from Table A where all corresponding items in Table B satisfy condition
I need to obtain all elements m_id of Table A where m_active is = N and that the corresponding elements in Table B have ALL v_active = N. 我需要获取表A的所有元素m_id,其中m_active为= N,并且表B中的相应元素具有ALL v_active =N。
m_id is foreign key in Table B. m_id是表B中的外键。
In the example below, what I am looking for is m_id=2 and and m_id=4 as both satisfy the condition of being m_active=N and have ALL v_active = N. 在下面的示例中,我要查找的是m_id = 2和m_id = 4,因为它们都满足成为m_active = N的条件,并且所有ALL v_active =N。
How do I go about that? 我该怎么办?
Thanks 谢谢
Table A example: 表A的示例:
m_id m_active
1 Y
2 N
3 Y
4 N
Table B example: 表B的示例:
v_id m_id v_active
1 1 N
2 1 Y
3 1 N
4 2 N
5 2 N
6 2 N
7 3 N
8 3 Y
9 3 Y
10 4 N
Try this: 尝试这个:
SELECT * FROM A
WHERE m_active='N'
AND NOT EXISTS (
SELECT * FROM B
WHERE B.m_id=A.m_id
AND B.v_active<>'N'
);
SELECT *
FROM a
WHERE m_active = 'N'
AND m_id NOT IN
(
SELECT m_id
FROM b
WHERE v_active <> 'N'
)
This will also select all entries from a
which have no corresponding entries in b
(and hence all 0
of 0
entries are inactive). 这也会从选择的所有条目
a
不具有相应的条目在b
(并因此所有0
的0
项是不活动的)。 This may or may not be what you want. 这可能是您想要的,也可能不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.