我想使用子查询来计算proceded_acct_creation应用程序,条件是主查询中的日期等于子查询中的日期,并且子查询中的应用程序ID必须在主查询中。问题是子查询也有很多应用程序ID。
申请人表
id | name
1 | John Doe
2 | Jane Doe
applicant_remarks表
id | applicant_id | status_id | updated_at
1 | 1 | 2 | 2019-04-10 11:40:12
2 | 2 | 2 | 2019-04-10 11:42:56
3 | 2 | 4 | 2019-04-10 11:46:21
预期输出
GROUP_CONCAT(applicants.id) | proceeded_acct_creation
1,2 | 1
我的输出
GROUP_CONCAT(applicants.id) | proceeded_acct_creation
1,2 | 0
我已经尝试过了:
SELECT
GROUP_CONCAT(applicants.id),
DATE(remarks.updated_at) AS `date`,
IFNULL(
(
SELECT COUNT(*)
FROM applicant_remarks
WHERE status_id = 4
AND DATE(updated_at) = DATE(remarks.updated_at)
AND applicant_id IN (GROUP_CONCAT(applicants.id))
GROUP BY DATE(remarks.updated_at)
),
0
) AS proceeded_acct_creation
FROM applicants
INNER JOIN applicant_remarks remarks ON remarks.applicant_id = applicants.id
WHERE remarks.status_id = 2
GROUP BY DATE(remarks.updated_at)
HAVING `date` IS NOT NULL
ORDER BY remarks.updated_at DESC
问题出在这部分:
IFNULL(
(
SELECT COUNT(*)
FROM applicant_remarks
WHERE status_id = 4
AND DATE(updated_at) = DATE(remarks.updated_at)
AND applicant_id IN (GROUP_CONCAT(applicants.id))
GROUP BY DATE(remarks.updated_at)
),
0
) AS proceeded_acct_creation,
applicant_id IN (GROUP_CONCAT(applicants.id))
必须
GROUP_CONCAT(applicant_id) IN (GROUP_CONCAT(applicants.id))
但返回错误:
Error Code: 1111. Invalid use of group function