gpt4 book ai didi

php - MySQL COUNT() + 半秒 COUNT

转载 作者:行者123 更新时间:2023-11-30 00:58:41 25 4
gpt4 key购买 nike

我有下表,我正在尝试计算“name”和“name2”列中的值,但如果“name2”!=null,则仅添加一半的计数值。并且像以前一样创建另一个列,它还检查“事件”值。

例如我的 table

ID  |  Name1 | Name2 |  Active 
------------------------------
1 | John | | True
2 | Mike | John | True
3 | Tim | | False
4 | Tim | Burt | False

我正在尝试创建结果

Names | Split_Count | Active_Count
Burt | 0.5 | 0
John | 1.5 | 1.5
Mike | 0.5 | 0.5
Tim | 1.5 | 0

到目前为止,我只能计算整数,那么我在 PHP 中这样做会更好吗?

非常感谢所有帮助。

编辑:为了澄清起见,查询将对名称 +1,但如果行中有 2 个名称,则对每个名称 +0.5。

其中,当 ACTIVE = TRUE 时,Active_Count 将执行与上面相同的操作。

最佳答案

我想我明白你在追求什么。尝试一下:

create table person
(
id int unsigned not null primary key auto_increment,
Name1 varchar(50) default null,
Name2 varchar(50) default null,
Active varchar(50) not null default 'True'
);

insert into person (Name1,Name2,Active) values ('John',null,'True');
insert into person (Name1,Name2,Active) values ('Mike','John','True');
insert into person (Name1,Name2,Active) values ('Tim',null,'False');
insert into person (Name1,Name2,Active) values ('Tim','Burt','False');

select person as Names,
sum(splitScore) as Split_Count,
sum(activeScore) as Active_Count
from
(
select Name1 as person,
case when Name2 is null then 1 else 0.5 end as splitScore,
case when Active='True' and Name2 is null then 1 when Active='True' and Name2 is not null then 0.5 else 0 end as activeScore
from person where Name1 is not null
union all
select Name2 as person,
case when Name1 is null then 1 else 0.5 end as splitScore,
case when Active='True' and Name1 is null then 1 when Active='True' and Name1 is not null then 0.5 else 0 end as activeScore
from person where Name2 is not null
) t
group by person
;

关于php - MySQL COUNT() + 半秒 COUNT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20333362/

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