简体   繁体   English

仅当主查询具有子查询返回的所有行时才返回

[英]Only return if main query has all the rows that subquery returns

I want to write a query that would return data if and only if it has all the ids that the subquery would return 我想编写一个查询,当且仅当它具有子查询将返回的所有ids ,该查询才返回数据

What I've tried: 我尝试过的

SELECT * FROM main_table WHERE id IN ( SELECT id FROM sub_table WHERE SOMECODITION)

but it returns once it finds at least one matching row in sub query which i do not want. 但一旦它在子查询中找到至少一个我不想要的匹配行,它就会返回。

Mainly I wanna be able to check if the main_table has all the ids that sub_table returns 主要是我想能够检查main_table拥有所有的idssub_table的回报

EDIT: 编辑:

the SOMECONDITION has nothing to do with main query SOMECONDITION与主查询无关

You can achieve that by using left join as well. 您也可以使用left join来实现。 Like for instance, let's take a small example main_table has ids 1,2,3 and sub_table has ids 1,2,5,6 (that matches SOMECONDITION ) so the following query, 例如像,让我们举一个小例子main_table具有IDS 1,2,3sub_table有IDS 1,2,5,6 (匹配SOMECONDITION ),所以下面的查询,

select s.id sid,m.id mid from 
sub_table s left join main_table m on s.id = m.id where s.SOMECONDITION

would result in: 会导致:

sid     mid
1       1
2       2
5       null
6       null

In this case main table does not have all the IDs that the sub query returns. 在这种情况下,主表并不具有子查询返回的所有ID。 You can make use of count to fetch results only if the count(sid) equals count(mid) , since count doesn't consider null values. 仅当count(sid)等于count(mid) ,才可以使用count来获取结果,因为count不考虑null值。 So your query would become. 因此您的查询将成为。

SELECT * FROM main_table 
WHERE id IN ( SELECT id FROM sub_table WHERE SOMECODITION)
AND
(select count(Distinct sid) 
from (select s.id sid,m.id mid from sub_table s left join main_table m on 
s.id = m.id where s.SOMECONDITION) )
= (select count(Distinct mid) 
from (select s.id sid,m.id mid from sub_table s left join main_table m on 
s.id = m.id where s.SOMECONDITION) )

This is rather painful in MySQL. 这在MySQL中是相当痛苦的。 The following gets the number of matches: 以下获取匹配数:

SELECT COUNT(DISTINCT id)
FROM main_table
WHERE id IN ( SELECT id FROM sub_table WHERE SOMECODITION);

If you only want to select the rows, I would suggest that you get the count in php and use conditional logic there. 如果只想选择行,我建议您在php中获取计数并在那里使用条件逻辑。 But, you can also do that in SQL: 但是,您也可以在SQL中执行此操作:

SELECT mt.*
FROM main_table mt
WHERE mt.id IN ( SELECT st.id FROM sub_table st WHERE SOMECONDITION) AND
      (SELECT COUNT(DISTINCT mt.id)
       FROM main_table mt
       WHERE mt.id IN (SELECT st.id FROM sub_table st WHERE SOMECONDITION)
      ) =
      (SELECT COUNT(DISTINCT st.id) FROM sub_table st WHERE SOMECONDITION);

You need an inner join in this case. 在这种情况下,您需要一个inner join

SELECT m.* FROM main_table INNER JOIN sub_table s ON m.id = s.id WHERE SOMECODITION

I assume the SOMECODITION contains only conditions against sub_table . 我假设SOMECODITION只包含对条件sub_table With SOMECODITION , it's guaranteed that the result from sub_table must meet the condition and with INNER JOIN , all id's from sub_table must be found in main_table in order to return something from main_table . 随着SOMECODITION ,它是保证从结果sub_table必须满足的条件和INNER JOIN ,从所有ID的sub_table必须被发现main_table为了从返回的东西main_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM