2016-11-30 30 views
0

我需要创建一个MySQL查询来搜索值,但我需要结果返回具有相同的content_id数字(它是未知的,直到搜索)。mysql select where列值是相同的(未知值)

这里是我的表:

content_id  item_id   tag_id 
1    4    2 
1    4    3 
1    4    4 
1    4    5 
2    5    7 
2    5    8 
3    6    8 
3    6    9 

而且目前这里是我的查询:

SELECT item_id FROM table WHERE tag_id IN (7,8) 

这将返回:

Array 
(
    [0] => stdClass Object 
     (
      [item_id] => 5 
     ) 

    [1] => stdClass Object 
     (
      [item_id] => 5 
     ) 

    [2] => stdClass Object 
     (
      [item_id] => 6 
     ) 

) 

但我需要它返回(因为的content_id = 2在这种情况下):

Array 
(
    [0] => stdClass Object 
     (
      [item_id] => 5 
     ) 

) 

我希望这是有道理的..

+1

则只需添加一个'和'条款和' group' – Ghost

回答

1

您需要收集的content_id成团,然后检查该集团数超过1

SELECT item_id FROM table group by content_id having count(content_id) > 1