2012-07-12 48 views
0

這是我目前的查詢工作正常,但我需要它只返回quantity_to_spawn> 0當我添加一個where子句它是給我一個無效的組的消息。MYSQL語法 - GROUP BY與條件

我很高興提供更多的信息,如果需要,但我不確定它在這一點上的必要性。 它適用於我正在寫的遊戲。

SELECT spawns.*, (quantity - COUNT(game_moblist.spawn_id)) AS quantity_to_spawn,mobs.*  
FROM spawns LEFT JOIN mobs USING (mob_id) LEFT JOIN game_moblist USING (spawn_id) GROUP BY 
spawn_id ORDER BY quantity_to_spawn DESC 

回答

2

嘗試添加having代替where

SELECT spawns.*, (quantity - COUNT(game_moblist.spawn_id)) AS quantity_to_spawn, 
    mobs.*  
    FROM spawns 
    LEFT JOIN mobs USING (mob_id) 
    LEFT JOIN game_moblist USING (spawn_id) 
    GROUP BY spawn_id 
    having quantity_to_spawn > 0 
    ORDER BY quantity_to_spawn DESC 
+0

這工作不錯謝謝 – Shawn 2012-07-12 19:03:03