Getting Max date from multiple table after INNER JOIN

20,653
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
Share:
20,653

Related videos on Youtube

user1504606
Author by

user1504606

Updated on April 19, 2020

Comments

  • user1504606
    user1504606 about 4 years

    I have two following tables

    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
    

    I want to get reault in mysql such that it take the row from table two with 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
    
  • user1504606
    user1504606 about 11 years
    I have to join two tables to get the reult
  • user1504606
    user1504606 about 11 years
    ID in first table is related to booking Id in second table