2013-04-21 110 views
1

表包含(PID,玩家,級別,月,季)從查詢中使用結果子查詢返回意外的結果

我試圖創建一個顯示每個球員,他們打出這幾個月的查詢,什麼他們的排名是那個月以及那個月有多少玩家。

這裏是我想什麼是每月0.1我如何得到這個正確

SELECT name as Name 
    , month as Month 
    , rank as Rank 
    , (select count(*) from results where month='month') as TotalPlayers 
FROM `results` 
WHERE season=CurrentSeason 
    and name="Player" 

我的子查詢返回結果?

+0

請提供一些示例數據和期望的輸出 – 2013-04-21 18:30:27

回答

0

只是猜測,但也許這就是你想要什麼:

SELECT name as Name 
    , month as Month 
    , rank as Rank 
    , TotalPlayers 
FROM `results` r 
JOIN (
    SELECT month 
     , COUNT(*) as TotalPlayers 
    FROM results 
    GROUP BY month 
    ) m 
ON m.month=r.month 
WHERE season=CurrentSeason 
    and name="Player" 
0

您的月0子查詢返回的結果,因爲month是一個整數,但'month'是一個字符串。 MySQL試圖將字符串轉換爲整數。因爲沒有前導數字,所以整數爲0.

相反,您希望使用別名來區分表results的兩個引用。這樣,你可以說這兩者是平等的。

SELECT name as Name, 
     month as Month, 
     rank as Rank, 
     (select count(*) from results r2 where r.month=r2.month) as TotalPlayers 
FROM `results` r 
WHERE season=CurrentSeason 
    and name="Player" 

子查詢的這種結構,因爲所述內子查詢是指表中的外部查詢被稱爲「相關子查詢」。