简体   繁体   中英

mySQL Count rows for each ID in a table

Database schema

----------------------------------------------------------
| id | killed | killed_by | killed_uuid | killed_by_uuid |
----------------------------------------------------------

killed and killed_by hold the entity type. eg. "Player", "Environment", "Creature". No specifics.

killed_uuid and killed_by_uuid are the userID's if a player is involved in the kill.

This table holds kills that happens on my game server. Each kill is stored in a separate row so there are no totals for each player.

I want to create the totals for each userID and create a leaderboard from them. So basically, count the rows that for each separate UserID.

I have tried using

select killed_by_uuid, count(id)
from kills
where killed='999' AND killed_by='999'
group by killed_by_uuid
order by count(id) desc

999 being the ID that belongs to a player entity kill NOT an actual USERID. But all I get is a single result set:

Array
(
    [0] => c676680f-98cb-4893-b1ba-ab5ab59fc272
    [killed_by_uuid] => c676680f-98cb-4893-b1ba-ab5ab59fc272
    [1] => 15
    [count(id)] => 15
)
select killer, count(killer) as total_kills
from kills
where killed='player' and killed_by not in (<list of environment ids>)
group by killer
order by total_kills desc, killer
SELECT COUNT(*) FROM KILLS WHERE KILLED=? AND KILLED_BY NOT IN (?, ?, ?, ?, ?, ...)

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