-2

我目前有当前的查询:

SELECT count(*) as number_of_items,
    if(first_name <> "", 1, 0)
    + if(last_name <> "", 1, 0)
    + if(current_avatar is not null, 1, 0)
    + if(username <> "" 
        AND email <> "" 
        AND gender <> "" 
        AND date_of_birth <> "" ,1,0) AS completeness 
FROM `user` HAVING (completeness = '4')

但这并没有按预期工作。似乎它们的完整性都为 2。如果我删除计数和有子句,它确实会看到所有不同的完整性计数(从 1 到 4)。有没有办法计算个人的完整性计数?

4

1 回答 1

2

你需要一个group by completeness才能工作:

SELECT count(*) as number_of_items,
    if(first_name <> "", 1, 0)
    + if(last_name <> "", 1, 0)
    + if(current_avatar is not null, 1, 0)
    + if(username <> "" 
        AND email <> "" 
        AND gender <> "" 
        AND date_of_birth <> "" ,1,0) AS completeness 
FROM `user`
GROUP BY completeness
HAVING (completeness = '4')
于 2012-10-15T07:52:16.980 回答