有一个名为"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基础不好, 想了半天也无果, 故发帖求教, 先谢谢大家了 :)
解决方案 »
- 大家都进来看下,
- 关于mysql触发器,有没有办法实现这样的功能?
- php5与mysql5.0数据写入中文乱码问题,在线等!
- 【在线等】网站数据库同步与镜像问题!
- 紧急求SQL2000语句一个,!!
- 求救。linux10下,安装mysql5.0的问题。
- 急,在线等!windows2000Sp4和mysql4.0.18
- mysql怎样启动日志?
- 怎样让存储过程只返回1个结果集
- 如何禁止自动提交?(是不是mysql中的自动提交被它的驱动屏蔽了,为什么有这this.con.setAutoCommit(false);也不行。)
- mysql_query()和 mysql_real_query()区别?
- 标准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