MySQL join two table with the maximum value on another field
You need to use two sub-queries like this:
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 | NAME | MOBILE | DATE | BALANCE |
---|---|---|---|---|
1 | ABC | 12345 | September, 20 2013 00:00:00+0000 | 300 |
2 | XYZ | 98475 | September, 21 2013 00:00:00+0000 | 600 |
See this SQLFiddle