Marante
Marante

Reputation: 59

SQL-statement for outputting two values from same column, how is this best done?

I'm trying to create an SQL-statement that outputs (depart_time, arrival_time, max_passengers, current_passengers and two values from the name-column in the second table).

I'm creating a travel webpage, where you can book trips between pre-decided cities. The problem I'm having at the moment, is that I can't output two values from the name column (eg. departure city and arrival city). When I try the code that is listed under "SQL-statements", the same city is outputted twice, as seen under the "Result" label.

The program lets you choose two cities (from a dropdown menu), and I save the variable in the code as "fromCity" and "toCity". The first one is "Lissabon", and the second one is "Madrid". However, when I run the statement, I get Lissabon listed twice.

I've been struggling for several hours now, and can't seem to find the correct solution the problem.

Any help is appreciated!

Best regards, Victor

TABLES:

Trips table

+---------+-------------+--------------+----------------+--------------------+
| trip_id | depart_time | arrival_time | max_passengers | current_passengers |
+---------+-------------+--------------+----------------+--------------------+
|       1 | 2016-07-20  | 2016-07-21   |             50 |                  0 |
|       2 | 2016-07-25  | 2016-07-26   |             70 |                  0 |
|       3 | 2016-07-26  | 2016-07-27   |             70 |                  0 |
|       4 | 2016-07-27  | 2016-07-28   |             70 |                  0 |
|       5 | 2016-07-29  | 2016-07-30   |             45 |                  0 |
|       6 | 2016-07-30  | 2016-07-30   |             45 |                  0 |
|       7 | 2016-07-31  | 2016-08-02   |             50 |                  0 |
|       8 | 2016-08-02  | 2016-08-04   |             80 |                  0 |
|       9 | 2016-08-04  | 2016-08-06   |             80 |                  0 |
+---------+-------------+--------------+----------------+--------------------+

Cities table

+---------+--------------+----------------+------------+--------------+
| city_id | currency     | country        | name       | lang         |
+---------+--------------+----------------+------------+--------------+
|       2 | Svensk Krona | Sverige        | Malmoe     | Svenska      |
|       3 | Svensk Krona | Sverige        | Stockholm  | Svenska      |
|       4 | Dansk Krona  | Danmark        | Koepenhamn | Danska       |
|       5 | Euro         | Tyskland       | Berlin     | Tyska        |
|       6 | Euro         | Spanien        | Madrid     | Spanska      |
|       7 | Euro         | Portugal       | Lissabon   | Portugisiska |
|       8 | Polsk Zloty  | Polen          | Warszawa   | Polska       |
|       9 | Euro         | Frankrike      | Paris      | Franska      |
|      10 | Euro         | Nederländerna  | Amsterdam  | Holländska   |
+---------+--------------+----------------+------------+--------------+

SQL-STATEMENTS:

SELECT trips.depart_time, trips.arrival_time, trips.max_passengers, 
trips.current_passengers, 
cities.name AS fromCity, cities.name AS toCity
        FROM trips
        INNER JOIN arrivals
        ON arrivals.trip_id = trips.trip_id
        INNER JOIN departures
        ON arrivals.trip_id = departures.trip_id
        INNER JOIN cities
        ON departures.city_id = cities.city_id
        WHERE departures.city_id = 7
        AND arrivals.city_id = 6
        AND trips.depart_time = "2016-07-20"
        GROUP BY name

RESULT:

+-------------+--------------+----------------+--------------------+----------+----------+
| depart_time | arrival_time | max_passengers | current_passengers | fromCity | toCity   |
+-------------+--------------+----------------+--------------------+----------+----------+
| 2016-07-20  | 2016-07-21   |             50 |                  0 | Lissabon | Lissabon |
+-------------+--------------+----------------+--------------------+----------+----------+

EDIT:

Thanks to Peter. M, the solution is:

SELECT trips.depart_time, trips.arrival_time, trips.max_passengers, 
    trips.current_passengers, c1.name, c2.name
            FROM trips
            INNER JOIN arrivals
            ON arrivals.trip_id = trips.trip_id
            INNER JOIN departures
            ON arrivals.trip_id = departures.trip_id
            INNER JOIN cities AS c1
            ON departures.city_id = c1.city_id
            INNER JOIN cities AS c2
            ON arrivals.city_id = c2.city_id
            WHERE departures.city_id = 7
            AND arrivals.city_id = 6
            AND trips.depart_time = "2016-07-20"
            GROUP BY c1.name

Upvotes: 3

Views: 75

Answers (1)

Peter M.
Peter M.

Reputation: 713

(discussed and resolved in the comments above)

Make two inner joins to the cities table.

see: MySQL INNER JOIN Alias

Upvotes: 1

Related Questions