MYSQL: select by two foreign keys from the same table

Mohammad

I have the following tables:

flights: id, idcompany, idplane, fromCity, toCity, takeoff...

companies: id, name

planes: id, name

cities: id, name

I want to declare the names of the two cities "fromCity, toCity" in the same query.

MY purpose of this action is get the cities names at once, so I can display them to the client, without any need to do another query to get the cities names.

here is my try:

SELECT f.id, f.takeoff, f.arrival, ct.name as fromCity, f.toCity, c.name as company, p.name as plane 
FROM flights f, companies c, planes p, cities ct 
WHERE f.idCompany = c.id AND f.idPlane = p.id AND f.fromCity = ct.id 
ORDER BY f.takeoff ASC

this query will return the name of the "fromCity" but the id of the "toCity", so what I can do to get the two names by the same query?

Pham X. Bach

You could use this:

SELECT 
    f.id, f.takeoff, f.arrival, ct1.name as fromCity
    ,ct2.name as toCity, c.name as company, p.name as plane 
FROM 
    flights f
inner join
    companies c
on
    f.idCompany = c.id 
inner join
    planes p
on
    f.idPlane = p.id
inner join
    cities ct1 
on
    f.fromCity = ct1.id 
inner join
    cities ct2
on
    f.toCity = ct2.id   
ORDER BY 
    f.takeoff ASC;

And try not using old-style-join from now on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mysql: using two foreign keys to the same table

MySQL two foreign key from same table

Select name of two foreign keys referring to same primary key table

two foreign keys to same primary key select statement MYSQL

mysql queries on two foreign keys referencing the same table

Entity Framework Code First - two Foreign Keys from same table

join on two foreign keys from same table in SQL

Select from one table with two foreign keys in a single query

Two foreign keys pointing to the same table / model

JPA Hibernate two foreign keys to the same table

(MySQL) 2 foreign keys referencing the same table

MySQL database with two foreign keys in one table

How to do a mysql select query on a table with two columns of foreign keys that relate to another table of names

Search from keys in foreign table - mysql

sql for a table with two foreign keys to same table causing Cartesian product

Table with two foreign keys pointing to the same column of another table

Select all foreign keys from the foreign key table in django

Select all from table where the rows has the highest id of all those with the same foreign keys

entity framework Invalid column name Id (two foreign keys from the same primary table)

How to use back_populates() in SQLAlchemy with two (or more) foreign keys from same table?

SQL Select Text for multiple foreign keys to lookup table in same row

EFCore Linq ThenInclude Two Foreign Keys To Same Table

Eloquent relation setup, two foreign keys to same table laravel 5.2

SQL query with two columns as foreign keys of the same table

EF 6 how to set two foreign keys to same table

Entity Framework Core Two Foreign Keys - Same Table

EF Core 2.2 - Two foreign keys to same table

two foreign keys on same table to display parent fields

Two foreign keys on same table: how to implement on delete cascade?