如何将select语句生成的结果作为sql中同一select语句的输入

如何将select语句生成的结果作为sql中同一select语句的输入,sql,Sql,嗨,朋友们,如何使用单个表将select语句生成的结果作为sql中同一select语句的输入,请看 下面显示的链接中的示例显示了如何使查询成为公共表表达式CTE,然后在对该CTE的查询中,可以再次加入该CTE USE AdventureWorks2008R2; GO WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level) AS ( -- Anchor member definition SELECT e.Manag

嗨,朋友们,如何使用单个表将select语句生成的结果作为sql中同一select语句的输入,请看

下面显示的链接中的示例显示了如何使查询成为公共表表达式CTE,然后在对该CTE的查询中,可以再次加入该CTE

USE AdventureWorks2008R2;
GO
WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level)
AS
(
-- Anchor member definition
    SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, 
        0 AS Level
    FROM dbo.MyEmployees AS e
    INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
        ON e.EmployeeID = edh.BusinessEntityID AND edh.EndDate IS NULL
    WHERE ManagerID IS NULL
    UNION ALL
-- Recursive member definition
    SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID,
        Level + 1
    FROM dbo.MyEmployees AS e
    INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
        ON e.EmployeeID = edh.BusinessEntityID AND edh.EndDate IS NULL
    INNER JOIN DirectReports AS d
        ON e.ManagerID = d.EmployeeID
)
-- Statement that executes the CTE
SELECT ManagerID, EmployeeID, Title, DeptID, Level
FROM DirectReports
INNER JOIN HumanResources.Department AS dp
    ON DirectReports.DeptID = dp.DepartmentID
WHERE dp.GroupName = N'Sales and Marketing' OR Level = 0;
GO

您可以根据自己的需求使用子查询逻辑

  select A.* from (select * from mytable) A

从表1中选择empId,其中emp_id=174;这里会出现一个或多个empIdone,我想使用与emp_id相同的结果,列表是第一个这样的结果,循环应该迭代这是你想要的。。从表1中选择emp_id,其中emp_id在表1中选择emp_id,其中emp_id=174。因此,现在您将再次在此处获得一个列表,因此该列表必须用作输入,而不是174。这就像java中的递归