有没有办法编写单个查询来检查一组行是否与一组值匹配?我需要匹配每组值一行,并且我想知道所有行是否匹配.我可以通过多个查询执行此操作,例如:
select * from tableName where (value1,value2) = ('someValue1','someValue2')
select * from tableName where (value1,value2) = ('someOtherValue1','someOtherValue2')
最佳答案
你可以尝试类似的东西:
select t.*
from tableName t
join (select 'someValue1' value1,'someValue2' value2 union all
select 'someOtherValue1','someOtherValue2') v
on t.value1 = v.value1 and t.value2 = v.value2
where 2=
(select count(distinct concat(v1.value1,v1.value2))
from (select 'someValue1' value1,'someValue2' value2 union all
select 'someOtherValue1','someOtherValue2') v1
join tableName t1
on t1.value1 = v1.value1 and t1.value2 = v1.value2)
如果您要检查大量值对,则可能更容易将它们插入临时表并使用上述查询中的临时表,而不是两个单独的硬编码虚拟表.