日期:2014-05-17  浏览次数:20734 次

求解SQL语句的写法问题出在哪里
SQL code

第一个语句
select name from org_member where id not in (select member_id from logon_log)



第二个语句

select 
  a.id,c.name,d.name,b.name,a.name,a.org_department_id,a.org_level_id,a.org_account_id,a.org_post_id,
  b.path,b.org_account_id,substr(b.path,0,(instr(b.path,'.',-1)-1))
from 
  org_member a left join org_department b on a.org_department_id=b.id  
                    left join org_account c  on c.id = a.org_account_id
                    left join org_department d on d.path = substr(b.path,0,(instr(b.path,'.',-1)-1)) 
                                                      and d.org_account_id = c.id
where a.id not in (select member_id from logon_log)
order by c.sort_id,b.sort_id,a.sort_id




第一个语句是最基础的查询,第二个语句是为了取得更多的相关信息作了详细的处理,但二个语句,我觉得的我写的总体意思是一样的,为什么二个语句执行出来结果不一样?第二个语句的结果会多呢?

------解决方案--------------------
有可能是三个left join 与where有什么冲突,看的不是很直观

建议改下,将org_department,org_account,org_department这三张表先关联起来组成表t,然后表org_member left join t
------解决方案--------------------
探讨
SQL code

第一个语句
select name from org_member where id not in (select member_id from logon_log)



第二个语句

select
a.id,c.name,d.name,b.name,a.name,a.org_department_id,a.org_level_id,a.org_ac……

------解决方案--------------------
select 
a.id,c.name,d.name,b.name,a.name,a.org_department_id,a.org_level_id,a.org_account_id,a.org_post_id,
b.path,b.org_account_id,substr(b.path,0,(instr(b.path,'.',-1)-1))
from 
org_member a ,org_department b , org_account c ,org_department d 
where a.id not in (select member_id from logon_log) and a.org_department_id=b.id
and and c.id = a.org_account_id and d.path = substr(b.path,0,(instr(b.path,'.',-1)-1)) and d.org_account_id = c.id
order by c.sort_id,b.sort_id,a.sort_id