我在组合这两个语句时遇到了麻烦。
我有一个festivalTable
,你可以在=> voteTable
上投票。在特殊的场合,你也可以有一个specialVoteTable
对于两个单独的表,我有:
select festivalId as festId, COUNT(festivalId) as total from Vote
where active = 'J' and FestivalId = 593
group by FestivalId
另一张表也是如此。现在我需要合并这两个结果,除了特殊投票需要乘以8,所以我可以将总数除以9。
因此,这两个独立的查询是
select festivalId as festId, COUNT(festivalId) as total from Vote
where active = 'J' and FestivalId = 593
group by FestivalId
select festivalId as festId, (COUNT(festivalId) * 8) as total from SpecialChartVote
where active = 'J' and FestivalId = 593
group by FestivalId
(尚未与festivalTable
结合使用。)
有什么意义吗?
发布于 2017-03-26 20:47:52
在子查询中使用union all
:
select festId, sum(total)/9 as total
from
(
select
festivalId as festId
, COUNT(festivalId) as total
from Vote
where active = 'J' and FestivalId = 593
group by FestivalId
union all
select
festivalId as festId
, (COUNT(festivalId) * 8) as total
from SpecialChartVote
where active = 'J' and FestivalId = 593
group by FestivalId
) u
group by festId
发布于 2017-03-26 20:58:33
按照编写的方式,您的查询只查找一个节日。这看起来很奇怪,但您可以通过执行以下操作来组合查询:
select 593 as festId,
( (select count(*) from Vote where active = 'J' and FestivalId = 593) +
(select count(*) * 8 from SpecialChartVote where active = 'J' and FestivalId = 593)
) / 9.0
如果你想获取多个节日的信息,SQLZims答案肯定更好。我只想告诉您,您可以通过执行以下操作来“组合”您的查询:
select (<query1> + <query2>)
https://stackoverflow.com/questions/43028890
复制相似问题