我在组合这两个陈述方面遇到了麻烦。
我有一个festivalTable
你可以在=>上投票voteTable
。对于特殊场合,您还可以拥有specialVoteTable
对于我有的2个单独的表:
select festivalId as festId, COUNT(festivalId) as total from Vote
where active = 'J' and FestivalId = 593
group by FestivalId
另一张表相同。现在我需要结合这两个结果,除了从特殊投票我需要将它乘以8,所以我可以将总数除以9。
所以2个单独的查询是
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
合并。)
有道理吗?
答案 0 :(得分:2)
在子查询中使用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
答案 1 :(得分:0)
您的查询,正如所写,只是在寻找一个节日。这看起来很奇怪,但您可以通过执行以下操作来组合查询:
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>)