我有两张桌子:
__Table R_____
ID.....CustID....DATE
1......1.........2000-01-01
2......2.........2000-01-02
3......2.........2000-01-03
4......2.........2000-02-22
5......1.........2000-03-23
__Table Customers______
ID....NAME
1.....Lucas
2.....Michael
请问,如何从每个客户的NAME返回最后一个DATE的SELECT? (这条路):
Lucas......2000-03-23
Michael....2000-02-22
您可以使用:
select c.name, max(r.date)
from table_customers c
join table_r r on r.custid = c.id
group by c.id, c.name;
select c.name, r.date
from table_customers c
join table_r r on r.custid = c.id
ORDER BY r.date DESC
LIMIT 2 ;