首页 新闻 会员 周边

求sql语句优化

0
悬赏园豆:100 [已解决问题] 解决于 2014-04-04 09:54

select t1.*,count(t1.productId) buyNum from


(select a.productId,a.name,b.city,c.endCity
from bc_product a                                                    --产品
inner join bc_tra b on b.traId=a.traId                          --旅行社
inner join bc_line c on a.productId=c.productId            --旅行线路
where a.status=1 and b.status=1) t1


left outer join bc_order t2 on ( t1.productId=t2.productId and t2.status in(2,3) )  


group by t1.productId
order by buyNum desc
limit 0,10

 

在mysql里,查找3个表的一点新信息,按销售量排序

主管说效率低,他要求不用联表,各位可有好方法?

问题补充:

他说 “我不倾向于联表,所有联表都是有问题的”,要求不用联表解决

whai的主页 whai | 初学一级 | 园豆:112
提问于:2014-04-03 15:21
< >
分享
最佳答案
0
select productId,count(productId) buyNum
from bc_product a      
where exists (select 1 from bc_tra b where b.traId=a.traId and b.status=1)
and exists (select 1 from bc_line  c where a.productId=c.productId)
and exists (select 1 from bc_order d where a.productId=d.productId and d.status in(2,3) )
and a.status=1
group by a.productId 
order by buyNum desc
limit 0,10

可以不?

收获园豆:80
冰牙 | 初学一级 |园豆:118 | 2014-04-03 16:22
其他回答(5)
0

4個表的信息都要,不联表怎么查?让他写写看,要么这样

 

select t1.*,count(t1.productId) buyNum from


(select a.productId,a.name,b.city,c.endCity
from bc_product a                                                    --产品
inner join bc_tra b on b.traId=a.traId                          --旅行社
inner join bc_line c on a.productId=c.productId            --旅行线路
where a.status=1 and b.status=1

and a.productId in(select productId from bc_order where status in(2,3) )

) t1

 

group by t1.productId
order by buyNum desc
limit 0,10

收获园豆:20
hexllo | 园豆:318 (菜鸟二级) | 2014-04-03 15:44
0

他意思是不是不要有子查询?

让领导把话说清楚

Albert Fei | 园豆:2102 (老鸟四级) | 2014-04-03 15:55
0

让领导写吧,万能的领导

lucika.zh | 园豆:62 (初学一级) | 2014-04-03 16:14
0

不用join连接的话,就用子查询了

秋壶冰月 | 园豆:5903 (大侠五级) | 2014-04-03 17:19
0

你的t2有什么用?我感觉没用上啊。莫非你要用这种方式,增加t1的数量?

幻天芒 | 园豆:37175 (高人七级) | 2014-04-03 19:04

t2用来统计销售量,有的产品没销售量就插null

count(t1.productId)也可写成 count(t2.productId),反正统计group by productId 的个数,我觉得用主键好

 

支持(0) 反对(0) whai | 园豆:112 (初学一级) | 2014-04-04 09:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册