打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
on和where的争夺战
今天遇到一个数据库查询问题:两个表之间的连接的on过滤和where过滤的问题。
一般来说是在on后面的条件是两个表之间连接条件,也就是两个表之间的对应字段等于起来;where后面的条件是用传进来的一些参数对上面连接过后的条件再过滤。如下:
select 1 from t_herotask ht inner join t_task t on ht.taskid=t.code
inner join t_taskobjective tbj on t.id=tbj.taskID and tbj.ItemType <> '2'
left outer join t_card card on ht.heroId=card.heroId and card.typeid = tbj.ItemCode
 where ht.heroId=@heroID and t.code=@taskID and card.CONTAINERTYPE=1
group by card.typeid, t.id having sum(CURRENTSTACKS) is NULL OR
(select sum(tbj.num) from t_taskobjective tbj where tbj.taskid=t.id and tbj.
itemcode=card.typeid and tbj.itemtype<>'2') > sum(CURRENTSTACKS))
 
但是这样不行必须要,必须要这样才行:
select 1 from t_herotask ht inner join t_task t on ht.taskid=t.code
inner join t_taskobjective tbj on t.id=tbj.taskID and tbj.ItemType <> '2'
left outer join t_card card on ht.heroId=card.heroId and card.typeid = tbj.ItemCode
and card.CONTAINERTYPE=1
 where ht.heroId=@heroID and t.code=@taskID
group by card.typeid, t.id having sum(CURRENTSTACKS) is NULL OR
(select sum(tbj.num) from t_taskobjective tbj where tbj.taskid=t.id and tbj.
itemcode=card.typeid and tbj.itemtype<>'2') > sum(CURRENTSTACKS))
上面的移上去的条件不是从外面传进来的值,而只是简单过滤一下这个字段,但是不知道什么原因,像下面这样把本来应该放在where后面的条件过滤放到on后面就不行。难道SQL语句的执行就是这样的?很是神奇。。。
 
 最后得出了一个结论:如果过滤的条件里没有用到外部传值就放在on后面,否则就放在where后,当然只是在表间连接的时候才会用on这个关键词哈!!
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
各类制作软件 工具下裁地址
提花衣
送你一张Card,很好玩哦!
微博
保暖披肩
高压电器防跳原理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服