简体   繁体   中英

MYSQl, select from two tables, count, with exception

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:

在此处输入图片说明

(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!

Give this a try,

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

Remember that Count(username <> 'valaki') is valid only in mysql , if you happen try on other RDBMS, you can do it like this,

COUNT(CASE WHEN username <> 'valaki' THEN 1 ELSE NULL END)

I think you should try something like this:

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

Your query is arbitrarily choosing one username to put on each row. Wouldn't you want to see all of them?

Just try the query bellow, You just need to add "AND users.username IS NOT NULL AND COUNT(username) > 1" to your where statement.

It will get you clean results of the data you need to check.

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

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM