0

我想从一个简单的数据库表中提取名称、#kills、#deaths。

表有

_from(杀手),_to(死者),然后是事件的窃听。在 pvp 的情况下,我们想计算这个人是死了还是被杀了,然后把它加到他们的总数中

这可以通过查询来完成,还是我必须编写脚本?这不起作用@ 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

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

预期输出:

| name |    num_kills | num_deaths |
---------------------------------
|  naez   |  2 |    1 | 
|  prince |  0 |    1 | 
| chuck   |  1 |    0 | 
| salty   |  0 |    1 | 
4

1 回答 1

2
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

于 2013-05-23T23:07:59.080 回答