有一个名为"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基础不好, 想了半天也无果, 故发帖求教, 先谢谢大家了 :)
解决方案 »
- 如何在执行sql后的导出的文件里显示字段名(具体见内容)
- mysql坑爹了,把我一个大活人折腾成白痴了。。。
- 关于MySQL 开发的一些基础性问题
- linux MYSQL怎么去修改数据库存放的位置
- SQL 语句问题:较复杂,谢谢
- postgresql 问题 call 存储过程 急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 如何启动MYSQL进程
- 再问mysqlimport问题,请高手指点
- 大家帮我来看一下这个查询语句!!万分感激!!
- MySQL安装失败怎么办
- 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