Anda perlu menggunakan dua sub-kueri seperti ini:
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;
Keluaran:
CID | NAME | SELULER | TANGGAL | SALDO |
---|---|---|---|---|
1 | ABC | 12345 | 20 September 2013 00:00:00+0000 | 300 |
2 | XYZ | 98475 | 21 September 2013 00:00:00+0000 | 600 |
Lihat SQLFiddle ini
Sunting
Seperti yang dibahas dalam komentar, kueri ini juga dapat ditulis hanya dengan satu subkueri:
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
Lihat SQLFiddle yang disesuaikan