我有桌子
UserName Readed HuynhNH True HuynhNH False HuynhNH True HaiNV False HaiNV True QuyetM False
得到结果:
UserName readed HuynhNH 2 HaiNV 1 QuyetM 0
我该怎么写sql?谢谢支持
答案 0 :(得分:1)
尝试 -
SUM(CASE WHEN Readed = 'True' THEN 1 ELSE 0 END)
答案 1 :(得分:0)
这取决于您的数据库,是Mysql吗?
你可以这么简单:
select username,
sum(readed)
from t
group by username;
测试数据:
create table t (
username char(20),
readed boolean
);
insert into t values('HuynhNH',true);
insert into t values('HuynhNH',false);
insert into t values('HuynhNH',true);
insert into t values('HaiNV',false);
insert into t values('HaiNV',true);
insert into t values('QuyetM',false);
insert into t values('MrSimpleMind',true);
insert into t values('MrSimpleMind',false);
参见SQLFiddle: