2017-01-11 114 views
2

我設法一起破解SQL查詢,它一直在工作,直到我意識到由於我們使用的插件刪除非ASCII名稱,數據庫中的某些成員具有空名稱。我在頁面上顯示這些統計信息,讓沒有姓名的人不能很好地工作。我知道如何在使用WHERE子句時不顯示NULL,但我不太確定何時不使用WHERE子句。SQL查詢不顯示空值

我有什麼迄今爲止 -

SELECT player_stats.player_name, COUNT(player_kill.killer) 
FROM player_stats 
LEFT JOIN player_kill ON player_stats.player_id = player_kill.killer 
GROUP BY player_stats.player_name 
HAVING COUNT(player_kill.killer) > 1 
ORDER BY COUNT(player_kill.killer) DESC; 
+0

左後JOIN;在GROUP BY之前。 – user3741598

+0

@ user3741598我已經添加WHERE player_stats.player_name IS NOT NULL,但它仍然顯示NULL值。 –

+0

使用'INNER JOIN'而不是'LEFT JOIN'。那麼你將不會得到任何沒有匹配的行,並且計數將始終爲'> 1'。 – Barmar

回答

0

WHERE條款去所有的JOIN條款和GROUP BY之間。如果WHERE player_name IS NOT NULL不起作用,則名稱可能爲空字符串,而不是NULL,因此您需要檢查該字符串。

SELECT s.player_name, COUNT(*) AS count 
FROM player_stats AS s 
INNER JOIN player_kill AS k ON s.player_id = k.killer 
WHERE s.player_name IS NOT NULL AND s.player_name != '' 
GROUP BY s.player_name 
ORDER BY count DESC; 

另外,如果你不想讓0計數,使用INNER JOIN而非LEFT JOIN

0

如果您不想更改您的查詢,您可以通過檢查它是否爲空來替換玩家名稱。

SELECT ISNULL(s.player_name, "RandomPlayer"), COUNT(*) AS count 
FROM player_stats AS s 
INNER JOIN player_kill AS k ON s.player_id = k.killer 
WHERE s.player_name IS NOT NULL AND s.player_name != '' 
GROUP BY s.player_name 
ORDER BY count DESC; 

https://msdn.microsoft.com/en-us/library/ms184325.aspx