![](/img/trans.png)
[英]Doctrine query: best approach to creating a select query joining three tables?
[英]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.