0
===========================
= id = model_id = property =
===========================
= 14 = 1        = 1       =
===========================
= 15 = 1        = 3       =
===========================
= 16 = 2        = 1       =

我有一个像上面这样的表,我只想选择 model_ids 属性等于 1 AND 3

如果你能告诉我怎么做,我会很高兴

4

4 回答 4

2

听起来你想要这个:

select model_id
from yourtable
where property in (1, 3)
group by model_id
having count(*) > 1;

请参阅带有演示的 SQL Fiddle

或者,您可以使用以下内容:

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)

请参阅带有演示的 SQL Fiddle

于 2012-11-06T11:43:22.783 回答
0

您可以使用IN子句:

SELECT `model_id`
  FROM `table`
  WHERE `property` IN (1, 3)
于 2012-11-06T11:21:38.553 回答
0
select model_ids from yourtable where property in(1,3)
于 2012-11-06T11:22:09.037 回答
0

试试这个:

SELECT DISTINCT model_ids 
FROM tableName 
WHERE property in (1,3) 
GROUP BY model_ids 
HAVING COUNT( DISTINCT property ) = 2
于 2012-11-06T11:38:50.927 回答