我想将此 EF 代码转换为 SQL 命令:var parents = db.Fuels.Select(A => A.Parent).Distinct();var data = db.Fuels.Where(B => !parents.Any(A => A == B.ID)) .OrderBy(A => A.Type);我尝试编写SQL命令,但它不起作用:select *from Fuel as awhere id not in (select id from Fuel as b where a.ID = b.Parent)表结构:ID INT PK,Name NVARCHAR(50) Checked,Parent INT FK表数据我想要这个输出
1 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
这是您正在寻找的查询:
;with parents as (select distinct parent as ID from @Fuel)
select * from @Fuel where id not in (select Id from @Fuel where Id in(select Id from
parents))
SQL 小提琴:https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=dc39433e40541b814936f3dfea725c6b
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报
0/150
提交
取消