魔兽世界声望怎么获得:on和where的争夺战

来源:百度文库 编辑:九乡新闻网 时间:2024/05/02 02:31:01
今天遇到一个数据库查询问题:两个表之间的连接的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这个关键词哈!!