user_id product_id
1309051512660220 1005
1309051512660220 1006
1309051512660221 1007
1309051512660221 1009
1309051512660223 1010
1309051512660223 1012
1309051512660225 1016
1309051512660225 1021
1309051512660229 1038
1309051512660313 1000
1309051512660313 1003
1309051512660313 1005我的意思是同一个 user_id 下面有多个不同的product_id ,想查询同时有product_id = 1005 1006 的user_id.我的语句如下select * from t1 a
where exists(select 1 from t1 where product_id = 1005 and user_id = a.user_id)
and product_id = 1006
或者创建临时表 再做 in。 前一个方法效率太慢。特别是上千万数据量的时候,后一方法涉及到创建表也比较麻烦,有没有相对比较简单的方法呢?请大大们指教。
1309051512660220 1005
1309051512660220 1006
1309051512660221 1007
1309051512660221 1009
1309051512660223 1010
1309051512660223 1012
1309051512660225 1016
1309051512660225 1021
1309051512660229 1038
1309051512660313 1000
1309051512660313 1003
1309051512660313 1005我的意思是同一个 user_id 下面有多个不同的product_id ,想查询同时有product_id = 1005 1006 的user_id.我的语句如下select * from t1 a
where exists(select 1 from t1 where product_id = 1005 and user_id = a.user_id)
and product_id = 1006
或者创建临时表 再做 in。 前一个方法效率太慢。特别是上千万数据量的时候,后一方法涉及到创建表也比较麻烦,有没有相对比较简单的方法呢?请大大们指教。
解决方案 »
- win7 安装oracle 10g 后需要主机身份验证,但是总是不能验证通过
- oracl-00913 值过多!!
- Oracle bitand 问题
- 两个关联的表关系应该怎么建
- 求助:触发器如何插入数据
- oracle批量插入性能问题
- 请问如何在unix下把oracle数据库中的表结构和数据导出成。sql的文本
- ORACLE 数据库 中存储过程的调用(菜鸟问)
- 执行shutdown命令时非正常关闭,数据库启动后无法连接,提示错误正在进行关闭,无法连接
- 自己编写一个函数,实现反转
- 两个库,一样的表,一个库的表数据改变了,如何更新另一个表?
- 大家帮忙看下,我写了个函数返回字符串,在sql中调用不起作用,高手帮忙
from t1 t
where t.product_id in(1005,1006);
union select user_id from t1 where product_id =1006;
where t.product_id in(1005,1006)
group by t.user_id having count(t.user_id) > 1;
楼主自己都只用了一个子查询,而你的这,子查询,子查询 group by......
intersect
select user_id from t where product_id=1006