我遇到了一个棘手的SQL,看看是否有人可以提供帮助
SELECT column1, column2 as highestNo
FROM tableA
INNER JOIN TableB
on TableA.tNo = TableB.Plan_tno
返回Result(它总是返回一行)
column1 highestNo
J111646912 201603010576
Select Column3, Column4
From TableB
Inner join TableC
On TableB.key1 = TableC.plan_key
Where TableB.Column3< highestNo(get it from last set)
返回的结果应为
Column3 Column4
201603010525 J111646547
201603010004 B233435353
201603010324 J435345445
201603010570 H345353535
如何将脚本和结果整合为一块而不是两块? 所以我可以返回一组结果。
答案 0 :(得分:1)
您可以使用CROSS JOIN
:
Select Column3, Column4, t.column1, t.highestNo
From TableB
Inner join TableC On TableB.key1 = TableC.plan_key
cross join (
SELECT column1, column2 as highestNo
FROM tableA
INNER JOIN TableB on TableA.tNo = TableB.Plan_tno
) as t
Where TableB.Column3 < t.highestNo
第一个查询返回的单个记录将简单地附加到第二个查询返回的其余记录中。
答案 1 :(得分:0)
只需使用标量子查询;
Select Column3, Column4
From TableB b Inner join
TableC c
On b.key1 = c.plan_key
Where b.Column3 < (select column2 as highestNo
from tableA a inner join
TableB b
on a.tNo = b.Plan_tno
);
你说子查询总是返回一行。否则,我建议在子查询中使用max(column2)
。