| id | label |
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| id | data |
| 1 | data1 |
| 2 | data2 |
| 3 | data3 |
| id | tagId | imageId |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
| 4 | 3 | 2 |
| 5 | 3 | 3 |
我试图找到所有标签共同的图像(image.id = 2)。
如果提供了标签ID 1,2和3,我该如何查询它?
(抱歉标题不好)
答案 0 :(得分:1)
这是一种方法:
select m.imageId
from mapping m
where tagId in (1, 2, 3)
group by m.imageId
having count(*) = 3; -- needs to match the number of tags in the `where` clause
答案 1 :(得分:1)
一种方法是对imageid进行分组并检查所需的tagid数量。
select imageid
from mapping_table
group by imageid
having count(distinct case when tagid in (1,2,3) then tagid end) = 3