同一表的复合查询

Freeid_shi 2009-04-30 01:32:52
从一个表(这个表中有子项,子项也有子项,树形)
要从一个表里查询idno为空的所有记录,及(idno不为空时,表示有子数据,也在这个表里)idno不为空时的子数据。
下面的查询比较麻烦,而且会有好几层的情况,要写很多,有没简洁的写法,请大家帮忙看看

select *from GsoM where mk_no=S001' and isnull(idno,'')=''
union all
select *from GsoM where mk_no in (select distinct idno from TF_BOM where mk_no=S001')
...全文
89 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Freeid_shi 2009-05-03
  • 打赏
  • 举报
回复
这个语句在SQL在执行吗?能不能解释一下?多谢
you_tube 2009-04-30
  • 打赏
  • 举报
回复
修正一下
;with Args 
(
select *from GsoM where mk_no=‘S001' and isnull(idno,'')=''
union all
select GsoM.* from GsoM ,Args where args.idno = GsoM .mk_no
)
you_tube 2009-04-30
  • 打赏
  • 举报
回复
;with Args 
(
select *from GsoM where mk_no=‘S001' and isnull(idno,'')=''
union all
select *from GsoM ,Args where args.idno = GsoM .mk_no
)
Freeid_shi 2009-04-30
  • 打赏
  • 举报
回复
?
Freeid_shi 2009-04-30
  • 打赏
  • 举报
回复
语句有语法错误?
消息 102,级别 15,状态 1,第 6 行
')' 附近有语法错误。
lg3605119 2009-04-30
  • 打赏
  • 举报
回复
;with Args as
(
select *from GsoM where mk_no='S001' and isnull(idno,'')=''
union all
select *from GsoM ,Args where args.idno = GsoM .mk_no
)
you_tube 2009-04-30
  • 打赏
  • 举报
回复
;with Args
(
select *from GsoM where mk_no=S001' and isnull(idno,'')=''
union all
select *from GsoM ,Args where args.idno = GsoM .mk_no
)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧