MySQL计数()+半秒计数


MySQL COUNT() + half second COUNT

我有下表,我正在尝试计算'name'和'name2'列的值,但如果'name2'',则只添加一半的计数值=无效的和以前一样,还创建了另一列,在其中它还检查"活动"值。

例如,我的表

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,但如果行中有两个名称,则对每个名称加0.5。

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

我想我明白你在追求什么。试试看:

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
;