我有一种情况,我正在构建一个派生的数据透视表,我希望能够根据各种标准对其进行子选择(实际上是制作数据透视表)。
所以...在伪它看起来像这样......
select
(select sum(score) from derivedTable where username like 'a%') as scoresForANames,
(select sum(score) from derivedTable where username like 'b%') as scoresForBNames
from
( select username, score from gameTable where gameId = 111) derivedTable
请注意,这是一个荒谬的简化示例,用于说明我正在追求的内容......我根本不需要解决这个问题......我只需要了解mySQL中是否有一种概念方法可以实现相同的目的结果
问题是外部选择中的子选择不可见derivedTable
。所以,我很好奇我如何能够达到相同的结果,或者我不得不编写单独考虑所有标准的子选择。
答案 0 :(得分:1)
您想要查询查询的方式是在select
子句中根本没有子查询:
select sum(case when username like 'a%' then score end) as scoresForANames,
sum(case when username like 'b%' then score end) as scoresForBNames
from (select username, score
from gameTable
where gameId = 111
) derivedTable;
当然,在这种情况下,您根本不需要派生表:
select sum(case when username like 'a%' then score end) as scoresForANames,
sum(case when username like 'b%' then score end) as scoresForBNames
from gameTable gt
where gameId = 111;