我想將此 EF 代碼轉(zhuǎn)換為 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)表結(jié)構(gòu):ID INT PK,Name NVARCHAR(50) Checked,Parent INT FK表數(shù)據(jù)我想要這個輸出
1 回答

qq_笑_17
TA貢獻1818條經(jīng)驗 獲得超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 關(guān)注
- 191 瀏覽
添加回答
舉報
0/150
提交
取消