更新时间:2022-06-08 21:26:26
由于在 WHERE
子句中具有 notes
表的过滤器,因此 JOIN
的行为类似于 INNER JOIN
,将其移至 JOIN
条件:
Since you have the filter for the notes
table in the WHERE
clause the JOIN
is acting like an INNER JOIN
, move it to the JOIN
condition:
SELECT
jobs.*,
MAX(notes.`timestamp`) AS complete_date
FROM jobs
LEFT JOIN notes
ON (jobs.id=notes.job_id)
AND (notes.type="complete" OR notes.type IS NULL)
WHERE (jobs.status="complete" OR jobs.status="closed")
GROUP BY jobs.id
ORDER BY complete_date ASC;
这也可以使用子查询来完成,因此您可以在子查询中应用注释过滤器:
This could also be done using a subquery, so you apply the notes filter inside the subquery:
SELECT
jobs.*,
n.complete_date
FROM jobs
LEFT JOIN
(
select job_id, MAX(`timestamp`) AS complete_date
from notes
where (type="complete" OR type IS NULL)
group by job_id
) n
ON (jobs.id=n.job_id)
WHERE (jobs.status="complete" OR jobs.status="closed")
ORDER BY complete_date ASC