2

我有两个下表

table 1)
ID |  HOTEL ID | NAME 
1       100      xyz 
2       101      pqr
3       102      abc


table 2)
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
1         1       2013-04-12        100
2         1       2013-04-14        120
3         1       2013-04-9          90
4         2       2013-04-14        100
5         2       2013-04-18        150
6         3       2013-04-12        100

我想在 mysql 中获取 reault,以便它从表 2 中获取具有 MAX DEPARTURE DATE 的行。

ID | BOOKING ID | DEPARTURE DATE | AMOUNT
2         1       2013-04-14        120
5         2       2013-04-18        150
6         3       2013-04-12        100
4

2 回答 2

10
SELECT  b.ID,
        b.BookingID,
        a.Name,
        b.departureDate,
        b.Amount
FROM    Table1 a
        INNER JOIN Table2 b
            ON a.ID = b.BookingID
        INNER JOIN
        (
            SELECT  BookingID, MAX(DepartureDate) Max_Date
            FROM    Table2
            GROUP   BY BookingID
        ) c ON  b.BookingID = c.BookingID AND
                b.DepartureDate = c.Max_date
于 2013-04-18T11:59:15.783 回答
-3

出色地,

SELECT * FROM `table2` ORDER BY `DEPARTURE_DATE` DESC LIMIT 0,1

应该有帮助

于 2013-04-18T11:57:40.087 回答