Du skal bruge to underforespørgsler som denne:
SELECT a.cid, a.name, a.mobile, b.date, b.balance
FROM account a
JOIN
(
SELECT b1.* FROM balance b1
JOIN
(
SELECT cid, MAX(Date) As maxDate
FROM balance
GROUP BY cid
) b2
ON b1.cid = b2.cid
AND b1.date = b2.maxDate
) b
ON a.cid = b.cid;
Output:
CID | NAVN | MOBIL | DATO | BALANCE |
---|---|---|---|---|
1 | ABC | 12345 | 20. september 2013 00:00:00+0000 | 300 |
2 | XYZ | 98475 | 21. september 2013 00:00:00+0000 | 600 |
Se denne SQLFiddle
Rediger
Som diskuteret i kommentarerne, kan denne forespørgsel også skrives med kun én underforespørgsel:
SELECT a.cid, a.name, a.mobile, b1.date, b1.balance
FROM account a
JOIN balance b1 ON a.cid = b1.cid
JOIN (
SELECT cid, MAX(Date) As maxDate
FROM balance
GROUP BY cid
) b2
ON b1.cid = b2.cid
AND b1.date = b2.maxDate
Se den justerede SQLFiddle