如果我使用此查询:
SELECT map.x,map.y,users.username,Count(username) as count
FROM map LEFT JOIN users ON (map.x=users.x AND map.y=users.y)
WHERE map.y BETWEEN 0 AND 2 AND map.x BETWEEN 0 AND 2
GROUP BY map.x,map.y
ORDER BY map.y DESC,map.x
我得到了这个结果:
(原始图片:http://i.stack.imgur.com/3AOgH.png)
(我不需要用户名,这里只是为了解释) 但我不想算例如“valaki”,所以我想得到这样的结果:
(原始图片:http://i.stack.imgur.com/URGFs.png)
但是使用这段代码我会得到完全不同的东西:
SELECT map.x,map.y,users.username,Count(username) as count
FROM map LEFT JOIN users ON (map.x=users.x AND map.y=users.y)
WHERE map.y BETWEEN 0 AND 2 AND map.x BETWEEN 0 AND 2 AND users.username!='valaki'
GROUP BY map.x,map.y
ORDER BY map.y DESC,map.x
此:
(原始图片:http://i.stack.imgur.com/pO1Iy.png)
(玩家不是'valaki'的地方,但我需要那些没有人的地方。)
如果有人可以修复我的查询,我会非常高兴。谢谢!
答案 0 :(得分:0)
尝试一下,
SELECT map.x, map.y,
users.username,
Count(username <> 'valaki') as totalCount
FROM map
LEFT JOIN users
ON map.x=users.x AND map.y=users.y
WHERE map.y BETWEEN 0 AND 2 AND map.x BETWEEN 0 AND 2
GROUP BY map.x,map.y
ORDER BY map.y DESC, map.x
请记住,Count(username <> 'valaki')
仅在mysql
中有效,如果您尝试使用其他RDBMS,则可以这样做,
COUNT(CASE WHEN username <> 'valaki' THEN 1 ELSE NULL END)
答案 1 :(得分:0)
我认为你应该尝试这样的事情:
select map.x, map.y, group_concat(users.username),
sum(case when username <> 'valaki' then 1 else 0 end) as SumNoValaki
from map left join
users
ON map.x=users.x AND map.y=users.y
WHERE map.y BETWEEN 0 AND 2 AND map.x BETWEEN 0 AND 2
GROUP BY map.x,map.y
ORDER BY map.y DESC, map.x
您的查询是随意选择一个用户名放在每一行上。你不想看到所有这些吗?
答案 2 :(得分:0)
只需尝试下面的查询,您只需要在您的where语句中添加“AND users.username IS NOT NULL AND COUNT(username)&gt; 1”。
它将为您提供需要检查的数据的清晰结果。
SELECT map.x,map.y,users.username,Count(username) as count
FROM map LEFT JOIN users ON (map.x=users.x AND map.y=users.y)
WHERE map.y BETWEEN 0 AND 2 AND map.x BETWEEN 0 AND users.username IS NOT NULL AND COUNT(username) > 1
GROUP BY map.x,map.y
ORDER BY map.y DESC,map.x
答案 3 :(得分:0)
尝试:
SELECT map.x,map.y,users.username,Count(username) as count
FROM map
LEFT JOIN users ON (map.x=users.x AND map.y=users.y AND users.username!='valaki')
WHERE map.y BETWEEN 0 AND 2 AND map.x BETWEEN 0 AND 2
GROUP BY map.x,map.y
ORDER BY map.y DESC,map.x