有一个名为"photo_tags"的表, 其中有[photo_id]以及[user_id]两个字段现有的数据如下:[photo_id] [user_id]
1 1
1 2
2 1
3 1
3 2
4 1
5 1
5 2
如果我只想筛选出[user_id]为1和[user_id]为2共有的[photo_id], 其实就是[user_id]为1和[user_id]为2合影的照片, 这样的sql应该怎么写呢? 小弟sql基础不好, 想了半天也无果, 故发帖求教, 先谢谢大家了 :)
1 1
1 2
2 1
3 1
3 2
4 1
5 1
5 2
如果我只想筛选出[user_id]为1和[user_id]为2共有的[photo_id], 其实就是[user_id]为1和[user_id]为2合影的照片, 这样的sql应该怎么写呢? 小弟sql基础不好, 想了半天也无果, 故发帖求教, 先谢谢大家了 :)
where 2=(select count(*) from photo_tags where photo_id=t1.photo_id
and user_id in(1,2));
where user_id in(1,2)
group by photo_id
having count(*)=2;
where user_id = 1 intersect select distinct photo_id from photo_tags
where user_id = 2;说明 intersect 为交集
from photo_tags
where user_id=1 or user_id=2
group by photo_id
having count(*)=2
from photo_tags a ,photo_tags b
where a.photo_id=b.photo_id
and a.user_id=1 and b.user_id=2