===========================
= id = model_id = property =
===========================
= 14 = 1 = 1 =
===========================
= 15 = 1 = 3 =
===========================
= 16 = 2 = 1 =
我有一个像上面这样的表,我只想选择属性等于 1 AND 3 的 model_ids
如果您能告诉我如何做,我会很高兴
最佳答案
听起来你想要这个:
select model_id
from yourtable
where property in (1, 3)
group by model_id
having count(*) > 1;
或者您可以使用以下内容:
select model_id
from yourtable t1
where property = 1
and exists (select model_id
from yourtable t2
where t1.model_id = t2.model_id
and property = 3)
关于MySQL通过引用一个数据字段来选择多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13249903/