2016-06-23 58 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