简体   繁体   English

试图获取简单数据库表的计数统计信息

[英]trying to get count statistics for simple db table

I want to extract name, # kills, # deaths from a simple database table. 我想从一个简单的数据库表中提取名称,#kills,#deaths。

The table has 桌子有

_from (killer), _to (person who died), and then a wiretap of the event. _from(杀手),_to(死亡的人),然后是事件的窃听。 In the case of pvp, we want to count if the person died or got a kill and add it to their total 在pvp的情况下,我们想要计算这个人是否死亡或者被杀死并将其添加到他们的总数中

Can this be done with a query or will I have to script something? 这可以通过查询完成,还是我必须编写脚本? This don't work @ all: 这不起作用@ all:

SELECT (select COUNT( _from ) from wiretaps group by _from) as num_kills, 
       (select COUNT( _to ) from wiretaps group by _to) as num_deaths, 
        _from as name
FROM wiretaps
WHERE message LIKE  "%PvP:%"

http://sqlfiddle.com/#!2/1e5d9/1 http://sqlfiddle.com/#!2/1e5d9/1

| _FROM |    _TO | MESSAGE | ID |
---------------------------------
|  naez |  salty |    PvP: |  1 |
|  naez | prince |    PvP: |  2 |
| chuck |   naez |    PvP: |  3 |

expected output: 预期产量:

| name |    num_kills | num_deaths |
---------------------------------
|  naez   |  2 |    1 | 
|  prince |  0 |    1 | 
| chuck   |  1 |    0 | 
| salty   |  0 |    1 | 
select name, sum(num_kills) num_kills, sum(num_deaths) num_deaths
from (
    select _from name, count(*) num_kills, 0 num_deaths
    from wiretaps
    where message like '%PvP:%'
    group by _from
    union all
    select _to name, 0 num_kills, count(*) num_deaths
    from wiretaps
    where message like '%PvP:%'
    group by _to) x
group by name

SQLFIDDLE SQLFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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