0

If i use this query:

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

I get this result:

enter image description here

(Original image: http://i.stack.imgur.com/3AOgH.png)

(I don't need the usernames, it is here just for the explanation) But i don't want to count for example "valaki", so i would like to get a result like this:

http://i.stack.imgur.com/URGFs.png

(Original image: http://i.stack.imgur.com/URGFs.png)

But with this code i get someting completely different:

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

This:

http://i.stack.imgur.com/pO1Iy.png

(Original image: http://i.stack.imgur.com/pO1Iy.png)

(Those places where players aren't 'valaki', but i need those to where nobody is.)

If somebody can fix my query, i would be very happy. Thank you!

4

4 回答 4

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
于 2013-02-08T16:14:00.480 回答
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

您的查询是任意选择一个用户名放在每一行上。你不想看到所有这些吗?

于 2013-02-08T16:05:40.357 回答
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)
于 2013-02-08T16:02:47.773 回答
0

只需尝试下面的查询,您只需在 where 语句中添加“AND users.username IS NOT NULL AND COUNT(username) > 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
于 2013-02-08T16:07:34.937 回答