简体   繁体   中英

sql Better way than intersection of inner-join

We have a table of 2 columns : ObjectName and ObjectColor. It is a many to many table. And we have a given set of colors {x, y, z,...}

We want to select the list of ObjectName that exists with all those given colors.

if we have N colors, I can imagine an sql query with N-1 intersection on this same table. Or we can also imagine N-1 self-joins on that table.

Is there a better solution ? Is there a particular solution with MySQL ?

THINKS

Here is one method:

select objectname
from t
where objectcolor in (x, y, z)
group by objectname
having count(*) = 3;

You need to adjust the "3" to match the size of the list.

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