我想通过此SELECT(主)传递Id
:
SELECT x.id, ............ FROM xx INNER JOIN xx ON xx = xx ........
(例如:100行)
到(孩子)
SELECT a,b from xxxxx..... where id in (x.id)
(例如:也是100行)
最终将两个输出合并在一起
我在想什么:
select z.id,...........,z.a,z.b
(
SELECT x.id, ............ FROM xx INNER JOIN xx ON xx = xx ........ (eg: 100 rows)
SELECT a,b from xxxxx..... where id in (x.id) (eg: also 100 rows)
) z
我的问题是如何将ID传递给其他选择。
找到了解决方案,但子行与主行不匹配,因为主表中的键(id)在子表中不存在:
SELECT z.id, sum(a), sum(b), z.xx, z.xx from xxxxx.....,(SELECT x.id, ............ FROM xx INNER JOIN xx ON xx = xx ........) as z where id in (z.id) group by z.id
我如何输出结果,例如当它检测到子代中不存在ID时,Sum(a)和sum(b)将显示为(-)。
PS:每个选择查询都将联接多个表。
样本表(200行):
Id,m,m1,m2,m3,m4,m5
1,...
2,...
3,...
4,...
5,...
6,...
孩子:
Id,c1,c2,c3,c4,c5
1,...
1,...
1,...
5,...
5,...
6,...
预期输出:
(将母版中的所有ID传递给孩子)
结果(也应200行):
Id,m1,m2,m3,SUM(c1),SUM(c2)
1,...
2,m1,m2,m3,-,-
3,m1,m2,m3,-,-
4,m1,m2,m3,-,-
5,...
6,...
已解决(脑功能故障,我很糟糕):
SELECT x.id, ............,AwaitingA, AwaitingB FROM xx INNER JOIN xx ON xx = xx ........
LEFT JOIN (
SELECT id, sum(a) as AwaitingA, sum(b) as AwaitingB from xxxxx..... group by ....) d
on x.id = d.id
答案 0 :(得分:0)
尝试如下
SELECT a,b from xxxxx..... where id in ( SELECT x.id FROM xx INNER JOIN xx ON xx = xx)
答案 1 :(得分:0)
我认为您想要一个子查询。
看起来像这样:
select * from a where a.id in (select id from b);
编辑:如果要过滤来自a的多个列,则必须执行多个子查询。
select * from a where a.id in (select id from b) and a.xx in (select xx from c)
答案 2 :(得分:0)
SELECT x.id into #IDTempTable where etc.
然后
SELECT a,b from xxx inner join #IDTempTable as tt on tt.id = xxx.id
您可以在解决方案中使用临时表吗?当然,是否获得相同数量的行取决于ID的唯一性。