gpt4 book ai didi

mysql - 尝试获取简单数据库表的计数统计信息

转载 作者:行者123 更新时间:2023-11-29 08:30:39 25 4
gpt4 key购买 nike

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

表中有

_from(凶手),_to(死者),然后是对该事件的窃听。在 PVP 中,我们想要计算该人是否死亡或被击杀,并将其添加到总数中

这可以通过查询来完成还是我必须编写一些脚本?这不起作用@所有人:

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 |

最佳答案

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

关于mysql - 尝试获取简单数据库表的计数统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16725236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com