当前位置: 代码迷 >> Sql Server >> 使用sql,怎么选择出没有子栏目,并且该栏目没有书本的行来
  详细解决方案

使用sql,怎么选择出没有子栏目,并且该栏目没有书本的行来

热度:56   发布时间:2016-04-25 01:15:06.0
使用sql,如何选择出没有子栏目,并且该栏目没有书本的行来
我有两个表为 栏目表Class(id,pid,ClassName) 和书名表book(classid,BookName)
pid为上一级的栏目id号(根目录为0),classid为书本所属栏目
现在我想通过SQL选出没有子栏目且该子栏目没有书本的列来,该怎么些这个SQL语句?

------解决方案--------------------
select a.*
from class as a left join book as b on a.id=b.classid
where not a.id in (select distinct pid from class) and b.classid is null

------解决方案--------------------
select *
from class a left join class b on a.id=b.pid 
left join book c on c.classsid=a.id
where b.id is null and c.classid is null
  相关解决方案