我还不太熟悉Oracle方式的SQL语句,请高手帮个忙吧!String sql ="select a.Id, a.Deptname, ";sql +="decode((select count(1) from Aclorganization b where b.parentid =a.id),'0','true','false') re ";sql +="from Aclorganization a where a.parentid = ? order by a.deptorder asc  ";

解决方案 »

  1.   

    select a.Id, a.Deptname, 
    decode((select count(1) from Aclorganization b where b.parentid =a.id),'0','true','false') re 
    from Aclorganization a 
    where a.parentid = ? 
    order by a.deptorder asc 通过表的自关联查询每个部门有没有下级部门,其中:
    1. decode为类型转换函数,如果子查询范围0,则返回True,否则为False
    2. a.parentid = ? 为查询条件,?是需要传入的条件值参数
    3. order by a.deptorder asc 按照deptorder升序排列
    4. 子查询select count(1) from Aclorganization b where b.parentid =a.id是按照b表的第一个字段进行Count计算,b.parentid =a.id是b表的上级ID等于a表的id
      

  2.   

    查询部门的id,和名称,另外有个标志字段(如果该部门有父级就显示false,没有就显示true)
      

  3.   


    re就是是否存在下级部门decode((select count(1) from Aclorganization b where b.parentid =a.id),'0','true','false')的别名啊
      

  4.   

    呵呵,如果re 写成 as re 就更直观了。
    楼主可能是不了解decode 函数的使用,可以查下帮助文档
      

  5.   

    对的,我以前用的是SQL Server数据库
      

  6.   

    你可以把SQL挖出来放到数据库里面执行下就知道了
      

  7.   

    先谢谢各位了,我就是还不熟悉Oracle式的写法,我知道怎么做的