我想在一个选择中结合两个查询,这个问题 如果我使用“联盟”都说不同数量的列
首先选择 -
select users.fromsite,count(*) from users where users.fromsite != '' and users.fromsite is not null group by users.fromsite;
第二次选择 -
select sum(transfers.mimgebis_before), sum(transfers.mimgebis_after), users.fromsite,count(*) from transfers join users on transfers.player_id = users.pid where transfers.player_id in (select users.pid from users where users.fromsite in (select users.fromsite from users where fromsite != '' )) and users.fromsite != '' and transfers.date_created between '2013-12-10 08:20:00' and '2015-12-10 08:30:06' group by users.fromsite limit 0,5;
第一个查询结果在这里:
|| *fromsite* || *count(*)* ||
|| fortunejack.de || 2 ||
|| google.com || 3 ||
|| youtube.com || 2 ||
第二个查询结果在这里:
|| *sum(transfers.mimgebis_before)* || *sum(transfers.mimgebis_after)* || *fromsite* || *count(*)* ||
|| 20000.000000000 || || fortunejack.de || 1 ||
|| 53335.000000000 || 2000.000000000 || google.com || 2 ||
抱歉我的英语不好。
答案 0 :(得分:1)
我认为你不想要union
- 这要求两个查询的列相同。我想你只需要一个left join
,你可以将第二个查询中的列追加到第一个查询中。像这样:
select fs.fromsite, fs.cnt1, t.val1, t.val2, t.cnt2
from (select u.fromsite, count(*) as cnt1
from users u
where u.fromsite <> '' and u.fromsite is not null
group by u.fromsite
) fs left join
(select sum(t.mimgebis_before) as val1, sum(t.mimgebis_after) as val2, u.fromsite, count(*) as cnt2
from transfers t join
users u
on t.player_id = u.pid
where t.player_id in (select users.pid from users where users.fromsite in (select users.fromsite from users where fromsite != '' )) and
u.fromsite <> '' and t.date_created between '2013-12-10 08:20:00' and '2015-12-10 08:30:06'
group by u.fromsite limit 0,5
) t
on t.fromsite = fs.fromsite;