0

有一个小的 MySql 问题,我希望有人可以看看。

当前的 Sql:

SELECT IFNULL(ua.total, 0) applications, 
       users_applications.job_id as job_id, 
       users.* 
FROM users_applications 
        LEFT OUTER JOIN users 
             on (users_applications.user_id = users.id) 
        LEFT OUTER JOIN ( 
                           SELECT COUNT(*) total, job_id, id 
                           FROM users_applications
                           GROUP BY job_id ) AS ua 
                  ON (users_applications.id = ua.id)

这很棒,它返回的内容如下:

applications  |  job_id  |  user_id  |
-------------------------------
3     |   1001    |   1001  |
0     |   1001    |   1002  |
0     |   1001    |   1003  |
1     |   1003    |   1004  |
1     |   1003    |   1005  |
0     |   1004    |   1006  |

尽管它正在计算该工作的申请数量,但它只针对第一行执行此操作,但我希望它出现在每一行中,而不是像目前那样在后续行中显示为 0。

非常感谢

4

1 回答 1

2

试试这个。

SELECT  c.totalCount,
        a.job_id,
        a.user_id
FROM    users_applications a
            LEFT JOIN users b
                ON a.user_ID = b.ID
            LEFT JOIN
                (
                    SELECT job_ID, count(*) totalCount
                    FROM user_applications
                    GROUP BY job_ID
                ) c on a.job_ID = c.jobID
于 2012-08-15T17:03:00.997 回答