求一SQL语句,有一表是自关联关系,-对多的关系
如表 table_test 包含三列 id(主键),parentId(外键),name,
我想查出四列,除了前面三列外,再加一列isExist判断当前的记录是否存在子数据,也就是关联的数据,有的话显示true,没有显示false,这条SQL怎么来写呢?
如表 table_test 包含三列 id(主键),parentId(外键),name,
我想查出四列,除了前面三列外,再加一列isExist判断当前的记录是否存在子数据,也就是关联的数据,有的话显示true,没有显示false,这条SQL怎么来写呢?
解决方案 »
- oracle如何用日期类型字段做唯一键
- 新手请教:关于sqldeveloper配置问题
- 急!cursor结果集值改变,并返回结果集。在线等,很难,高手进,答出来立即给分。
- oracle9i中有没有将某个字段设置为自动增加的功能,急……
- 急!急!急!如何Recover UNDO表空间数据文件——在线等待!
- Oralce9i改IP后监听起不来
- 如何将查询数据库中的结果输出到文本?
- Oracle问题:执行cd $ORACLE_HOME/Apache/Apache/bin...........
- 这个SQL怎么写?
- SQL练习 分析函数 求答案
- 救急,同志们!系统连不上中心数据库了!
- in到exists
select id, parentId, name ,
case when t.parentId=t.id then 'true'
else 'false'
end isExist,
from table_test t
select id,parentID,name,decode(nvl(b.id,''),'',false,true) as isExist
from table_test a,table_test b
where a.parentID=b.id(+)
select id, parentId, name ,
case when exixts(select * from table_test where parentId=a.id)
then 'true'
else 'false'
end isExist,
from table_test a
case when exixts(select * from table_test where parentId=a.id)
then 'true'
else 'false'
end isExist
from table_test a
错了,两个表的话查询字段名不能那么写
而且,oracle里没有布尔型数据,true及false得加上''符号以字符串储存
case pid
when null then 'false'
else 'true' end isExist
from table_test a left join (select parentid pid from table_test group by parentid) b on a.id = b.pid
select id,parentID,name,decode(b.id,null,'false','true') as isExist
from table_test a,table_test b
where a.parentID=b.id(+)
7楼:
select id, parentId, name ,
case when exists(select 1 from table_test where parentId=a.id)
then 'true'
else 'false'
end isExist
from table_test a
效率差不多,写法比较简洁,其余不用考虑了