向子查询添加附加列会出错,要求对具有多个字段的子查询使用 'EXISTS'
Adding additional column to subquery gives error requesting to use 'EXISTS' for subqueries with more than one field
正如标题所描述的,我有一个查询可以完美地工作,但是我想从子查询的 table 中引入一个额外的列,这目前给我以下错误:
"You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field."
是否可以在子查询中引入多个列?或者加入会更好吗?我当前的代码如下:
SELECT S.Prod, S.Date, (SELECT Price,
FROM [Products] P
WHERE P.[Prod1] = S.[Prod]
AND P.[Date to use] = (SELECT MIN(P2.[Date to use])
FROM [Products] P2
WHERE P2.[Prod1] = P.[Prod1]
AND P2.[Date to use] > S.[Date])) AS Price
FROM [Sales] AS S;
我想引入一个名为 'Cost' 的字段,下面是我返回错误的错误代码:
SELECT S.Prod, S.Date, (SELECT Price, Cost
FROM [Products] P
WHERE P.[Prod1] = S.[Prod]
AND P.[Date to use] = (SELECT MIN(P2.[Date to use])
FROM [Products] P2
WHERE P2.[Prod1] = P.[Prod1]
AND P2.[Date to use] > S.[Date])) AS Price
FROM [Sales] AS S;
有人可以告诉我如何去做吗?干杯!
你可以从这个开始...
SELECT S.Prod, S.Date, P.Price, P.Cost
FROM [Sales] S, [Products] P
WHERE P.[Prod1] = S.[Prod]
AND P.[Date to use] = (SELECT MIN(P2.[Date to use])
FROM [Products] P2
WHERE P2.[Prod1] = S.[Prod]
AND P2.[Date to use] > S.[Date]);
正如标题所描述的,我有一个查询可以完美地工作,但是我想从子查询的 table 中引入一个额外的列,这目前给我以下错误:
"You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field."
是否可以在子查询中引入多个列?或者加入会更好吗?我当前的代码如下:
SELECT S.Prod, S.Date, (SELECT Price,
FROM [Products] P
WHERE P.[Prod1] = S.[Prod]
AND P.[Date to use] = (SELECT MIN(P2.[Date to use])
FROM [Products] P2
WHERE P2.[Prod1] = P.[Prod1]
AND P2.[Date to use] > S.[Date])) AS Price
FROM [Sales] AS S;
我想引入一个名为 'Cost' 的字段,下面是我返回错误的错误代码:
SELECT S.Prod, S.Date, (SELECT Price, Cost
FROM [Products] P
WHERE P.[Prod1] = S.[Prod]
AND P.[Date to use] = (SELECT MIN(P2.[Date to use])
FROM [Products] P2
WHERE P2.[Prod1] = P.[Prod1]
AND P2.[Date to use] > S.[Date])) AS Price
FROM [Sales] AS S;
有人可以告诉我如何去做吗?干杯!
你可以从这个开始...
SELECT S.Prod, S.Date, P.Price, P.Cost
FROM [Sales] S, [Products] P
WHERE P.[Prod1] = S.[Prod]
AND P.[Date to use] = (SELECT MIN(P2.[Date to use])
FROM [Products] P2
WHERE P2.[Prod1] = S.[Prod]
AND P2.[Date to use] > S.[Date]);