[英]MySql count from other table as join and add to every row of results
Got a small MySql problem I was hoping someone could take a look at. 得到一个小的MySql问题我希望有人可以看看。
Current Sql: 当前的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)
Which is great and it's returning something like: 哪个很棒,它会返回类似于:
applications | job_id | user_id |
-------------------------------
3 | 1001 | 1001 |
0 | 1001 | 1002 |
0 | 1001 | 1003 |
1 | 1003 | 1004 |
1 | 1003 | 1005 |
0 | 1004 | 1006 |
Although it is counting the number of applications for that job it's only doing it for the first row, I'd like it to appear in every row instead of just a 0 for subsequent rows as it is at the moment. 虽然它正在计算该作业的应用程序数量,但它只是在第一行中进行,我希望它出现在每一行中,而不是仅仅为后续行显示为0。
Many thanks 非常感谢
Try this one. 试试这个吧。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.