日期:2014-05-19  浏览次数:20426 次

困惑已久的问题,关于right join 和where的冲突
例如
select   *   from   t1
inner   join   t2   on   ....
inner   join   t3   on   ....
right   join   t4   on   ....
where   t1.name= '... '   and   t1.date= '... '
就是说,我的预期是保留表t4所有记录,但是where条件会过滤掉t4的记录,而where又是t1的条件筛选

我现在的一个办法是把t4放在from后面,使用left   join,但还有没有其他的办法呢?
谢谢!


------解决方案--------------------
不是where的问题,是执行时的顺序就是这样的
------解决方案--------------------
要么这样
select * from (select * from t1
inner join t2 on ....
inner join t3 on .... where t1.name= '... ' and t1.date= '... ') t
right join t4 on ....

------解决方案--------------------
select * from
(
select * from t1
inner join t2 on ....
inner join t3 on ....
where t1.name= '... ' and t1.date= '... '
) as t
right join t4 on .... --注意这里....中对t1 t2 t3的引用都要改成t



------解决方案--------------------

--用子查询
select * from
(select * from t1 where name= '... ' and date= '... ') A
inner join t2 on ....
inner join t3 on ....
right join t4 on ....


------解决方案--------------------
1 2楼好快
------解决方案--------------------
哼哼~~
------解决方案--------------------
这样试试看,先把123连好,然后再right join T4,

select * from
(select * from t1 where t1.name= '... ' and t1.date= '... '
inner join t2 on ....
inner join t3 on ....) tt
right join t4 on ....



------解决方案--------------------
晕,
都是霹雳闪电手啊~,迅雷不及掩耳盗铃~
------解决方案--------------------
先把表1筛选玩再联接呗