0

我从事一个使用 SQL Server 的 C# 项目。

我想在一个查询中加入多个表(或在一个查询中运行多个语句)

我的查询是这样的:

(select HR.ID, HR.Cod, HR.CodeValed, H.Onvan, H.Model from HR LEFT JOIN H ON HR.ID_Sefat = H.ID) AAA;
SELECT GG.Cod G
FROM 
(Select * From AAA Where AAA.Model = 'M') MM
Left JOIN (Select * From AAA Where AAA.Model = 'K') KK ON MM.CodeValed = KK.Cod
Left JOIN (Select * From AAA Where AAA.Model = 'G') GG ON KK.CodeValed = GG.Cod

但它不起作用,它返回一个错误。

解决此问题的最佳方法是什么?

4

1 回答 1

2

尝试这个 :

with AAA as 
(select HR.ID, HR.Cod, HR.CodeValed, H.Onvan, H.Model from HR 
LEFT JOIN H ON HR.ID_Sefat = H.ID) 
SELECT GG.Cod G
FROM 
(Select * From AAA Where AAA.Model = 'M') MM
Left JOIN (Select * From AAA Where AAA.Model = 'K') KK ON MM.CodeValed = KK.Cod
Left JOIN (Select * From AAA Where AAA.Model = 'G') GG ON KK.CodeValed = GG.Cod
于 2013-06-14T08:37:03.270 回答