MySQL 查询三个表,一个自引用且具有相同的字段。最好的方法是什么?
MySQL query with three tables, one self referenced and with same fields. What's the best approach?
我正在尝试编写一个查询来汇集来自三个 table 的数据:
---------------------------------
| 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 |
-----------------------------------------------------------------
我面临的问题基本上有两个:目标的自动引用 table(我可以使用别名轻松解决)以及我试图合并来自的两组数据这一事实两个不同的选择(我想用临时 tables 来解决)。
现在,如果不是某些列具有相同的名称,那一切都很好。因为我想在我的临时 table 中保留 "ids" (与链接一起使用)我无法创建带有通配符的临时 table (例如 SELECT * FROM),但我必须拼出所有列并编写一个可怕的查询。它会工作,但它不灵活,如果我以后添加其他列,它会更新它!
任何 MySQL 大师都可以提出更好的方法吗?
来自澳大利亚的感谢和欢呼。
当您构建报告时,使用 SELECT 通常不是一个好主意 *,
因为当我修改 table 时,报告会显示错误的结果。
通常我更喜欢用 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
你好...
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 |
+---------+--------------+----------+------------+----------+-------+
我正在尝试编写一个查询来汇集来自三个 table 的数据:
---------------------------------
| 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 |
-----------------------------------------------------------------
我面临的问题基本上有两个:目标的自动引用 table(我可以使用别名轻松解决)以及我试图合并来自的两组数据这一事实两个不同的选择(我想用临时 tables 来解决)。
现在,如果不是某些列具有相同的名称,那一切都很好。因为我想在我的临时 table 中保留 "ids" (与链接一起使用)我无法创建带有通配符的临时 table (例如 SELECT * FROM),但我必须拼出所有列并编写一个可怕的查询。它会工作,但它不灵活,如果我以后添加其他列,它会更新它!
任何 MySQL 大师都可以提出更好的方法吗?
来自澳大利亚的感谢和欢呼。
当您构建报告时,使用 SELECT 通常不是一个好主意 *,
因为当我修改 table 时,报告会显示错误的结果。
通常我更喜欢用 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
你好...
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 |
+---------+--------------+----------+------------+----------+-------+