1

我有 2 张桌子,书和 temp_table。book : 中有 2 列book_id and tag_id,而 temp_table ( tag_id) 中只有 1 列。

我需要使用以下规则进行选择:

  • 如果 book 具有 temp_table 中的所有标签,它将在结果中。

例如,有 3 本书。

   First one has tag_id: 1,2,3,4.
   Second - 1,3,4
   Third - 1,2,3,5

并且 temp_table 包含 tag_id: 1,3,4

需要的选择必须包含第一和第二本书。3d 书不能在结果集中,因为它的 tag_id = 5,而 temp_table 中没有。

4

1 回答 1

1
select book_id
from book b
inner join temp_table t on t.tag_id = tag_id
group by book_id
having count(distinct tag_id) = (select count(distinct tag_id) from temp_table)
于 2012-11-10T14:05:18.873 回答