I want to find the latest date
from b.filed
that matches the a.id from table b, and then add this column to the result set.
Table a
Table b
Expected result
Try
SELECT
a.id,
a.NAME,
b.date
FROM
a
LEFT JOIN LATERAL (
SELECT * FROM b WHERE b.id = a.id ORDER BY date DESC LIMIT 1
) AS b
ON b.id = a.id
But my MySQL version is 5.7, which does not support LATERAL, so I used
SELECT
a.id,
a.NAME,
( SELECT b.date FROM b WHERE a.id = b.id ORDER BY b.date DESC LIMIT 1
) AS date
FROM
a