在下面的表中执行这段SQL如下 
select * from sys_questiondir where parentid in(1,10,12,14,236,237,242,243,238,239,240)现在要的效果是如何过滤掉parentID=0并且该项没有子项的部分(及下级的parentID=上级的ID)
上图中蓝色部分的ID没有作为任何的parentID,所以想去掉
就是查询出的数据中去掉图片中蓝色选中的部分
请大侠们帮写下这个sql..要简单点..
我要转换为linq
别写什么with之类的什么函数啊,就要一段短点的sql..谢谢.

解决方案 »

  1.   

    等高手来写不用函数的和不用CTE的
      

  2.   

    呵呵,因为我要转为linq...写cte没用
      

  3.   

    ??
    DELETE [Table]  WHERE ParentId=0 AND NOT exists(SELECT 1 FROM [Table] a WHERE a.Parentid=[Table].Id)
      

  4.   

    上面的语句中[Table]表示的是你图中的数据,含义是删除parentid=0并且它没有儿子的记录
      

  5.   

    楼主的意思好像是和BOM差不多的吧
    猜一下:SELECT * FROM sys_questiondir WHERE parentid IN(1,10,12,14,236,237,242,243,238,239,240)
    AND NOT EXISTS (SELECT 1 FROM sys_questiondir a WHERE a.FParentID=ID)
      

  6.   

    还是我自己搞定了很简单的撒.select * from sys_questiondir where parentid in(10,12,14,2,16,17,18,19,20,22,1)
    or id  in(10,12,14,2,16,17,18,19,20,22,1)
      

  7.   

    打错,应该是:SELECT * FROM sys_questiondir WHERE parentid IN(1,10,12,14,236,237,242,243,238,239,240)
    AND EXISTS (SELECT 1 FROM sys_questiondir a WHERE a.FParentID=ID)