簡體   English   中英

有什么方法可以從派生表中進行子選擇嗎?

[英]Is there any way to do a subselect from a derived table?

我遇到一種情況,我正在構建派生的數據透視表,然后希望能夠基於各種條件對它進行子選擇(實際上是制作數據透視表)。

所以...用偽裝看起來像這樣...

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對於外部選擇中的子選擇不可見。 因此,我很好奇如何獲得相同的結果,或者是否不得不編寫必須單獨考慮所有標准的子選擇。

想要用短語表達查詢的方式是在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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM