I'm working with 3 table. Driver
, Car
, and Garage
. Garage is a 1 to N
relationship table between Driver
and Car
(One driver has at least one car)
Driver Table
╔════╦══════╗
║ ID ║ Name ║
╠════╬══════╣
║ 1 ║ Edson║
║ 2 ║ Tomas║
║ 3 ║ Carry║
╚════╩══════╩
Car Table
╔════╦════════╗
║ ID ║ Brand ║
╠════╬════════╣
║ 1 ║ Ferrari║
║ 2 ║ Porche ║
║ 3 ║ McLaren║
╚════╩════════╩
Garage Table
╔════╦═════════╗═══════
║ ID ║Driver.Id║Car.Id║
╠════╬═════════╣═══════
║ 1 ║ 1 ║ 1 ║
║ 2 ║ 2 ║ 2 ║
║ 3 ║ 2 ║ 3 ║
║ 4 ║ 3 ║ 2 ║
╚════╩═════════╩═══════
I need get a Drivers list with your cars. Something like this:
[Edson, [Ferrari]]
[Tomas, [Porche, McLaren]]
[Carry, [Porche ]]
I already tried the code below, but I didn't get the expected result.
connection.execute(db.select([Driver.Name, Car.Brand]).where(Garage.Driver.Id == Driver.ID and Garage.Car.Id == Car.ID)).fetchall()
So, how can I make JOIN query to get the expected results?