2016-06-23 36 views
1

我收到以下查询,如果他们在他们的配置文件中输入字段,将显示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

只需添加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 
+0

你是我的天赐救世主。当你想尽一切办法解决(为了我的观点)困难的问题时,有时思考简单是非常困难的 – Adam