0

我有以下表格:

users
+----------+----------+----------+
| id       | name     | dob      |
+----------+----------+----------+

authentications
+----------+----------+----------+ 
| id       | user_id  | provider |
+----------+----------+----------+

一个用户,有许多身份验证。user_idauthentications表中是表的外键users

我试图完成以下输出:

+---------------+---------------+---------------+---------------+ 
|    user.id    |   user.name   |   user.dob    |       C       |
+---------------+---------------+---------------+---------------+

在这里,C 是提供者为“facebook”的用户身份验证计数。如果不是计数,我可以得到一个布尔值,即如果存在身份验证,则为 true,如果不存在,则为 false,那会更好。

我不知道如何构建查询。任何指针将不胜感激。

4

4 回答 4

2

使用用户和身份验证之间的 JOIN 创建查询,并首先在 User Id 字段中添加 GROUP BY。您的 group by 将需要包含您希望在结果中列出的后续字段,尽管它们不会改变结果的效果:

SELECT u.id, u.name, u.dob, COUNT(a.user_id) as authCount
FROM users u 
LEFT JOIN authentications a on u.id = a.user_id
WHERE a.provider = 'facebook'
GROUP BY u.id, u.name, u.dob
于 2012-05-23T14:44:16.953 回答
0

C 是计数,如果有 facebook 提供者,B 将返回 1,如果没有,则返回 0(布尔值)。更新为使用 LEFT OUTER JOIN 以便包括所有用户。

SELECT 
   users.id,
   users.name,
   users.DOB,
   SUM(CASE WHEN authentications.provider = 'facebook' then 1 else 0 end) as C,
   MAX(CASE WHEN authentications.provider = 'facebook' then 1 else 0 end) as B
FROM users
LEFT OUTER JOIN authentications
   ON users.id = authentications.user_id
GROUP BY users.id, users.name, users.DOB
于 2012-05-23T14:43:20.437 回答
0

1)这将返回每个用户的 FB 类型授权的实际计数:

`

Select Name UserName, ID UserID, COUNT(FBAuth) FBAuthCount
from Users LEFT JOIN
(Select UserID, COUNT(AuthType) FBAuth from Auth 
Where AuthType = 'FB'
Group by USERID) Auths on (Users.ID = Auths.UserId)
Group by ID, Name

`

2) 这将为每个用户返回标志 true 或 false,具体取决于他们的授权类型:

`

Select Name UserName, ID UserID, 
CASE WHEN COUNT(FBAuth) > 0 THEN 'TRUE' ELSE 'FALSE' END FBAuthFLAG
from Users LEFT JOIN 
(Select UserID, COUNT(AuthType) FBAuth from Auth 
Where AuthType = 'FB'
Group by USERID) Auths on (Users.ID = Auths.UserId)
Group by ID, Name

`

在较小的数据集上使用连接:更快的响应。最后,我相信这是一个:

`

SELECT users.id,
       users.email,
       users.name,
       users.sid,
       users.created_at,
       ( CASE
           WHEN fb_auth > 0
           THEN 'true'
           ELSE 'false'
         END ) AS facebook
FROM   users
       LEFT JOIN (SELECT authentications.user_id,
                         Count(authentications.provider) fb_auth
                  FROM   authentications
                  WHERE  provider = 'facebook'
                  GROUP  BY user_id) auths
              ON ( users.id = auths.user_id )
GROUP  BY users.id,
          users.email,
          users.name,
          users.sid,
          users.created_at

`

于 2012-06-21T10:23:08.883 回答
-1

较早的答案有效,但速度极慢。该查询过去需要 15-20 分钟才能运行。一位朋友建议的这个运行速度要快得多(同一数据集约 4 秒)。

SELECT users.id,
       users.email,
       users.name,
       users.sid,
       users.created_at,
       ( CASE
           WHEN fb_auth > 0
           THEN 'true'
           ELSE 'false'
         END ) AS facebook
FROM   users
       LEFT JOIN (SELECT authentications.user_id,
                         Count(authentications.provider) fb_auth
                  FROM   authentications
                  WHERE  provider = 'facebook'
                  GROUP  BY user_id) auths
              ON ( users.id = auths.user_id )
GROUP  BY users.id,
          users.email,
          users.name,
          users.sid,
          users.created_at;
于 2012-06-19T06:58:33.260 回答