簡體   English   中英

MySQL查詢包含三個表,一個表是自引用的,並且具有相同的字段。 最好的方法是什么?

[英]MySQL query with three tables, one self referenced and with same fields. What's the best approach?

我正在嘗試編寫一個查詢來匯總三個表中的數據:

---------------------------------
|   destinations                |
---------------------------------
| id    | city       | status   |
---------------------------------
| 1     | Milan      | Open     |
| 2     | Florence   | Open     |
| 3     | Venice     | Open     |
---------------------------------


---------------------------------
|   trips                       |
---------------------------------
| id    | from  | to    | train |
---------------------------------
| 1     | 1     | 2     | 2     |
| 2     | 1     | 2     | 3     |
| 3     | 2     | 1     | 2     |
| 4     | 2     | 3     | 2     |
| 5     | 1     | 3     | 1     |
| 6     | 3     | 1     | 1     |
---------------------------------


---------------------------------
|   trains                      |
---------------------------------
| id    | train                 |
---------------------------------
| 1     | T1                    |
| 2     | ChooChoo              |
| 3     | IC123                 |
---------------------------------

我的想法是,我希望能夠向我的用戶顯示開始或結束(例如佛羅倫薩)的所有旅程。

遵循以下原則:

-----------------------------------------------------------------
|         Query: all trains going to/from Florence              |
-----------------------------------------------------------------
| trips.id | from.id | from (city) | to.id | to. city   | train |
-----------------------------------------------------------------
| 1        | 1       | Milan       | 2     | Florence   | 2     |
| 2        | 1       | Milan       | 2     | Florence   | 3     |
| 3        | 2       | Florence    | 1     | Milan      | 2     |
| 4        | 2       | Florence    | 3     | Venice     | 2     |
-----------------------------------------------------------------

我面臨的問題本質上有兩個:目標的自動引用表(我可以使用別名輕松解決)和我試圖合並來自兩個不同選擇的兩組數據(我想解決這些問題)的事實。與臨時表)。

現在,如果不是有些列具有相同的名稱,那就太好了。 因為我想在臨時表(與鏈接一起使用)中保留“ id”,所以無法創建帶有通配符的臨時表(例如SELECT * FROM),但是我必須拼寫出所有列和編寫一個可怕的查詢。 它可以工作,但不會靈活,如果以后再添加其他列,將很難更新它!

任何MySQL專家都可以建議一種更好的方法嗎?

感謝和澳大利亞的歡呼。

當您生成報告時,通常不建議使用SELECT *,因為當我修改表時,報告可能會顯示錯誤的結果。

通常我更喜歡用id_content來寫id字段,例如。 id_destination,id_to,id_train,ecc ...

SELECT trips.id, f.id, f.city, t.id, t. city, trains.train 
FROM trips trips
INNER JOIN destinations f
ON trips.from = f.id
INNER JOIN destinations t
ON trips.from = t.id
INNER JOIN trains trains
ON trips.train = trains.id
ORDER BY 1 ASC

G'day ...

DROP TABLE IF EXISTS destinations;

CREATE TABLE destinations
(city_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,city    VARCHAR(12) NOT NULL UNIQUE
,status  VARCHAR(12) NOT NULL
);

INSERT INTO destinations VALUES
(1,'Milan','Open'),
(2,'Florence','Open'),
(3,'Venice','Open');

DROP TABLE IF EXISTS trips;

CREATE TABLE trips
(trip_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY 
,from_city_id INT NOT NULL
,to_city_id INT NOT NULL
,train INT NOT NULL
,UNIQUE(from_city_id,to_city_id,train)
);

INSERT INTO trips VALUES
(1,1,2,2),
(2,1,2,3),
(3,2,1,2),
(4,2,3,2),
(5,1,3,1),
(6,3,1,1);

DROP TABLE IF EXISTS trains;

CREATE TABLE trains
(train_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,train VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO trains VALUES
(1,'T1'),
(2,'ChooChoo'),
(3,'IC123');

SELECT * FROM destinations;
+---------+----------+--------+
| city_id | city     | status |
+---------+----------+--------+
|       1 | Milan    | Open   |
|       2 | Florence | Open   |
|       3 | Venice   | Open   |
+---------+----------+--------+

SELECT * FROM trips;
+---------+--------------+------------+-------+
| trip_id | from_city_id | to_city_id | train |
+---------+--------------+------------+-------+
|       1 |            1 |          2 |     2 |
|       2 |            1 |          2 |     3 |
|       5 |            1 |          3 |     1 |
|       3 |            2 |          1 |     2 |
|       4 |            2 |          3 |     2 |
|       6 |            3 |          1 |     1 |
+---------+--------------+------------+-------+

SELECT * FROM trains;
+----------+----------+
| train_id | train    |
+----------+----------+
|        2 | ChooChoo |
|        3 | IC123    |
|        1 | T1       |
+----------+----------+

SELECT t.trip_id
     , t.from_city_id
     , from_city.city
     , t.to_city_id
     , to_city.city
     , t.train 
  FROM trips t 
  JOIN destinations from_city 
    ON from_city.city_id = t.from_city_id 
  JOIN destinations to_city 
    ON to_city.city_id = t.to_city_id 
 WHERE 'Florence' IN(from_city.city,to_city.city);
+---------+--------------+----------+------------+----------+-------+
| trip_id | from_city_id | city     | to_city_id | city     | train |
+---------+--------------+----------+------------+----------+-------+
|       3 |            2 | Florence |          1 | Milan    |     2 |
|       4 |            2 | Florence |          3 | Venice   |     2 |
|       1 |            1 | Milan    |          2 | Florence |     2 |
|       2 |            1 | Milan    |          2 | Florence |     3 |
+---------+--------------+----------+------------+----------+-------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM