0

车辆

+--------------------+--------------+------+-----+---------+
| Field              | Type         | Null | Key | Default | 
+--------------------+--------------+------+-----+---------+
| id                 | int(11)      | NO   | Pk  | NULL    |       
| model              | varchar(35)  | NO   |     | NULL    |            
+--------------------+--------------+------+-----+---------+

信息

+--------------------+--------------+------+-----+---------+
| Field              | Type         | Null | Key | Default | 
+--------------------+--------------+------+-----+---------+
| id                 | int(11)      | NO   | Pk  | NULL    |       
| vehicle_id         | varchar(35)  | NO   | FK  | NULL    |       
| location           | varchar(35)  | NO   |     | NULL    |       
+--------------------+--------------+------+-----+---------+

轴 1

+--------------------+--------------+------+-----+---------+
| Field              | Type         | Null | Key | Default | 
+--------------------+--------------+------+-----+---------+
| id                 | int(11)      | NO   | Pk  | NULL    |       
| vehicle_id         | varchar(35)  | NO   | FK  | NULL    |       
| weight             | varchar(35)  | NO   |     | NULL    |       
+--------------------+--------------+------+-----+---------+

轴 2

+--------------------+--------------+------+-----+---------+
| Field              | Type         | Null | Key | Default | 
+--------------------+--------------+------+-----+---------+
| id                 | int(11)      | NO   | Pk  | NULL    |       
| vehicle_id         | varchar(35)  | NO   | FK  | NULL    |       
| weight             | varchar(35)  | NO   |     | NULL    |       
+--------------------+--------------+------+-----+---------+

我希望创建一个查询,该查询将返回具有公共vehicle_id 的所有表中的所有字段。车辆 ID 是 3 个表(信息、轴 1、轴 2)中每个表中对车辆表中主键的引用。有人可以解释一下我该怎么做吗?我尝试使用多个连接,但它没有用!非常感谢。

编辑:

我试过的查询是;

SELECT * 
      FROM  Vehicle
      JOIN info, axle1, axle 2
      ON vehicle.id = axle1.vehicle_id 
     AND vehicle.id = axle2.vehicle_id AND vehicle.id = info.vehicle_id 
4

2 回答 2

2

试试这个:

SELECT *
FROM Vehicle v
INNER JOIN Info   i ON          v.id = i.vehicle_id
INNER JOIN Axle1 a1 ON i.vehicle_id  = a1.vehicle_id
INNER JOIN Axle2 a2 ON a1.vehicle_id = a2.vehicle_id 
于 2012-09-05T13:41:30.850 回答
0

检查此链接:mySQL get information from multiple tables in a query我认为有一个可能适合的示例

您只需要为每个表指定一个标识符,这样您就可以在引用字段时指定您正在谈论的表。然后,确保将所有 id 作为条件链接,以便省略标识符不匹配的所有组合。那将是:

SELECT * FROM Vehicle v, Info i, Axle1 a1, Axle2 a2 WHERE v.id == a1.vehicle_id AND v.id == a2.vehicle_id AND v.id == i.vehicle_id

于 2012-09-05T13:43:19.570 回答