我有一张名为' users'我的用户拥有4个不同的登录选项:passwd,facebook_login,google_login和aprofiel_login。 我想创建一个获得结果的查询:
我提出了以下查询,它给出了前5个,但我无法得到计算倍数的那个。
我的查询是这样的:
select count(*) total,
sum(case when passwd is not null and (facebook_login is null and google_login is null and aprofiel_login is null) then 1 else 0 end) gad,
sum(case when facebook_login is not null and (passwd is null and google_login is null and aprofiel_login is null) then 1 else 0 end) facebook,
sum(case when google_login is not null and (passwd is null and facebook_login is null and aprofiel_login is null) then 1 else 0 end) google,
sum(case when aprofiel_login is not null and (passwd is null and facebook_login is null and google_login is null) then 1 else 0 end) aprofiel
from `users`
where auth_level = 1
and is_account_active = 1
然后我考虑过,我有我的总金额,我有所有其他单一的金额,如果我用所有其他的减去总数,我有多个登录用户的结果。 但是我如何在MySQL中做到这一点?
答案 0 :(得分:1)
将此查询粘贴到子查询中,然后对派生列进行数学运算:
SELECT
t1.*,
total - (gad + facebook + google + aprofiel) as otherfield
FROM
(
select count(*) total,
sum(case when passwd is not null and (facebook_login is null and google_login is null and aprofiel_login is null) then 1 else 0 end) gad,
sum(case when facebook_login is not null and (passwd is null and google_login is null and aprofiel_login is null) then 1 else 0 end) facebook,
sum(case when google_login is not null and (passwd is null and facebook_login is null and aprofiel_login is null) then 1 else 0 end) google,
sum(case when aprofiel_login is not null and (passwd is null and facebook_login is null and google_login is null) then 1 else 0 end) aprofiel
from `users`
where auth_level = 1
and is_account_active = 1
) t1