简体   繁体   中英

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.

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). 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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