select t.* from t_customer_tel t
where exists
(select 1 from t_product p,t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id
);
--结果记录为 29 select t.*--count(*)
from t_customer_tel t, t_product p, t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id;
--结果记录为 108这2个sql为什么差距这么大呢,2个sql条件一样,为什么结果数量不对呢?
where exists
(select 1 from t_product p,t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id
);
--结果记录为 29 select t.*--count(*)
from t_customer_tel t, t_product p, t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id;
--结果记录为 108这2个sql为什么差距这么大呢,2个sql条件一样,为什么结果数量不对呢?
解决方案 »
- ORACLE里的用户名是什么,怎么修改
- ORA-01033: ORACLE initialization or shutdown in progress
- 多表查询,有点难
- AIX严重问题!!!急
- rowID 应用问题
- 存储过程传参报PLS-00201问题。
- oracle连接12514错误,但采用oracle的net manager测试连接成功?
- 请问大家 如何恢复数据啊。。很急在线等 。。
- 求Oracle存储过程的电子书
- ROWID的错误,请进............................................
- 高手详细解释一下优化SQL查询的几种方法
- oracle replace 高级应用
因为这两个表连接产生了重复的customer_tel_id
因此结果大大增加你可以确认一下
select count(1),count(distinct p.customer_tel_id)
from t_customer_tel t, t_product p, t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id;
最大记录数为t_customer_tel 记录数第二条
最大记录数为笛卡尔集
where exists
(select 1 from t_product p,t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id
);
--结果记录为 29其实这种写法相当于:select * from t_customer_tel
where customer_tel_id in
(select customer_tel_id
from t_product p,
t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
)
这样选出来的记录自然是不会超过t_customer_tel记录的条数。但是下面的查询时先做的是等值连接。是在笛卡尔积的基础上,判断p.customer_tel_id 和t.customer_tel_id相等的记录。这样自然就会多。比如p里面有两条记录的是同一个customer_tel_id,和T表做等值连接就会有两条记录。这就是原因!
select t.*--count(*)
from t_customer_tel t, t_product p, t_query_recommend r
where p.product_status='2'
and p.product_spec_id in ('A101','A102')
and p.product_id=r.product_id
and p.customer_tel_id=t.customer_tel_id;
--结果记录为 108
第一个SQL用exists 意思是后面 select 1 from……查询出的结果在 t_customer_tel 表中存在的记录,t_customer_tel 表中不匹配的不列出来。
第二个SQL就是表关联,三个表中每条记录都关联一下,数据当然多了。