小组中的计数总和

时间:2016-06-23 12:06:53

标签: mysql count group-by sum

我有以下查询,如果他们在个人资料中输入字段,则显示0或1。

select id, 
    count(users.firstname),
    count(users.lastname),
    count(users.gender),
    count(users.country),
    count(users.address_one),
    count(users.city),
    count(users.zipcode),
    count(users.housenumber),
    count(users.phonenumber),
    count(users.educationlevel_id)
from users
group by id;

如何将所有计数加起来并按ID分组?

select id, SUM(
    count(users.firstname)+ 
    count(users.lastname)+ 
    count(users.gender)+
    count(users.country)+
    count(users.address_one)+
    count(users.city)+
    count(users.zipcode)+
    count(users.housenumber)+
    count(users.phonenumber)+
    count(users.educationlevel_id)
    ) as smartbio_check
from users
group by id, smartbio_check;

此查询无法正常工作

1 个答案:

答案 0 :(得分:1)

只需添加count值:

select id, 
    count(users.firstname) +
    count(users.lastname) +
    count(users.gender) +
    count(users.country) +
    count(users.address_one) +
    count(users.city) +
    count(users.zipcode) +
    count(users.housenumber) +
    count(users.phonenumber) +
    count(users.educationlevel_id) as smartbio_check
from users
group by id