次のようなものを試してください。
SELECT (SELECT MAX(DATE)
FROM (SELECT DATE
FROM PAYMENTS P
WHERE P.INVOICE_ID = IN.INVOICE_ID
UNION
SELECT DATE
FROM SENDS S
WHERE S.INVOICE_ID = IN.INVOICE_ID) SUB) MAX_DATE,
IN.*
FROM INVOICE IN
ORDER BY 1 DESC
LIMIT 5
アップデート:
副選択の第2レベル内ではテーブルの請求書にアクセスできないため、上記のSQLは機能しません。
この選択を試してください:
SELECT V.*
FROM verkoopfacturen AS V
LEFT JOIN (SELECT invoice_id,
MAX(datum) as max_date
FROM (SELECT P.invoice_id,
P.date AS datum
FROM invoice_payments AS P
UNION
SELECT S.file_id,
S.datum AS datum
FROM email AS S) SUB
GROUP BY SUB.invoice_id) SUB2 on V.Verkoopfact_InfId = SUB2.invoice_id
ORDER BY SUB2.MAX_DATE DESC
LIMIT 5