mysql - Count 2 columns for occurrence -


so basically, require query return display name, amount of kills , amount of deaths.

i have 2 tables need pull from.

the 2 tables are

player

id      | name 2334324 | user 4353454 | user2 

where id unique identifier , name display name.

the second table is:

player_kill

id | killer  | victim  | 1  | 2334324 | 4353454 | 2  | 2334324 | 4353454 |   3  | 4353454 | 2334324 | 

where killer / victim columns contain unique identifier of player table.

i'd able count occurrences of player id in killer , victim query returns:

name | kills | deaths user | 2     | 1 user2| 1     | 2 

where number under kills amount of occurrences playerid has in killer column , same deaths

hope provided enough information.

what have far:

select `player`.`name`, count(distinct `player_kill`.`id`) `kills`, count(distinct `player_kill`.`id`) `deaths` `player` left join `player_kill` on `player`.`id`=`player_kill`.`killer` left join `player_kill` on `player`.`id`=`player_kill`.`victim` `player`.`id` = `player_kill`.`killer` , `player`.`id` = `player_kill`.`victim` group `player`.`id`; 

try

select      p.name,      count(distinct pk1.id) kills,      count(distinct pk2.id) deaths player p left join player_kill pk1 on pk1.killer = p.id  left join player_kill pk2 on pk2.victim = p.id  group p.name 

http://sqlfiddle.com/#!9/649504/15/0


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -