我有一个查询:
SELECT DISTINCT
countryName,countrySlug
FROM countries AS Country
INNER JOIN countries_networks AS n ON Country.id = n.country_id
AND n.network_id IN (1,14)
效果很好。但是,我现在需要在其中添加一个“必须拥有”子句,以便n.network_id也必须位于集合(6,7,8,9)中。 (顺便说一句,它们可以有多个network_id,因为我是直接从查找表中提取的。)
所以我尝试添加另一个IN():
SELECT DISTINCT
countryName,countrySlug
FROM countries AS Country
INNER JOIN countries_networks AS n ON Country.id = n.country_id
AND n.network_id IN (1,14)
AND n.network_id IN (6,7,8,9)
现在完全不返回任何结果。
这似乎是我在这里犯了一个愚蠢的错误。谁能看到它是什么?
是;您要求network_id
存在于两个不相交的列表中。给定值a
不能同时出现在以下两个列表中:
1, 14
6, 7, 8, 9
为了击败一匹死马,让我们看一下每个值
value list1 list2
------------------
1 x
6 x
7 x
8 x
9 x
14 x
这是两个列表中的全部值;该范围之外的任何内容均不符合oth]条件,并且该范围内的任何值均不符合both条件。
为了满足单个条件Country
可以基于network_id
关联表具有多个country_network
的条件,您可以这样做:
select distinct
c.countryname, c.countryslug
from country c
join country_network cn1 on cn1.country_id = c.country_id
join country_network cn2 on cn2.country_id = c.country_id
where cn1.network_id in (1, 14) and cn2.network_id in (6, 7, 8, 9)
是
根据我的以上评论,请尝试以下操作:
[IN ()
是一系列OR的同义词