简体   繁体   English

SQL INNER JOIN来自同一表的两个值数组

[英]SQL INNER JOIN two arrays of values from the same table

I have a simplified query where I just need to get the intersection of two arrays. 我有一个简化的查询,我只需要获取两个数组的交集即可。 When there is a positive intersection, I am getting the expected result returned. 当有一个正交点时,我会得到预期的结果。 But if there is no result, I get all rows returned from the entire table, as opposed to the expected zero rows. 但是,如果没有结果,我将从整个表中返回所有行,而不是预期的零行。

This is one query where I get one row returned, as expected: 这是一个查询,我按预期返回了一行:

SELECT ID FROM table_name WHERE ID IN (7188,2442,10350) AND ID IN (10350,6936)

It returns one row with the ID 10350, as expected. 如预期的那样,它将返回ID为10350的一行。

But if I try this query: 但是,如果我尝试此查询:

SELECT ID FROM table_name WHERE ID IN (7188,2442) AND ID IN (10350,6936)

it returns the entire table, instead of zero rows. 它返回整个表,而不是零行。

Clearly, my syntax is incorrect, but I don't know how to write the proper query that will find the intersection of two arrays. 显然,我的语法不正确,但是我不知道如何编写适当的查询来查找两个数组的交集。 Can I please find out how to write such an intersection the correct way? 请问如何正确地写出这样的路口? Please notice that this is only one table (as opposed to an INNER JOIN from multiple tables). 请注意,这只是一个表(而不是来自多个表的INNER JOIN)。 I have to use multiple arrays as the sets to be compared. 我必须使用多个数组作为要比较的集合。

I've searched SO and Google for a long time, but nothing seems to come up. 我已经搜索了很长时间的SO和Google,但似乎什么都没有。 It seems like there should be a simple answer to this. 似乎应该对此有一个简单的答案。

I was running this query and getting an empty result after all. 我正在运行此查询,并最终获得空结果。 But then I sent the result immediately to a WordPress function, which happens to return all rows if you call an empty array as one of the values in $args. 但是随后我将结果立即发送到WordPress函数,如果您将空数组作为$ args中的值之一调用,该函数会返回所有行。

When I saw your comments that the finding wasn't being replicated, I realized I needed to isolate the problem further. 当我看到您的评论说该发现没有被复制时,我意识到我需要进一步隔离问题。 So those comments helped me solve the problem. 因此,这些评论帮助我解决了问题。

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

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