车辆
+--------------------+--------------+------+-----+---------+
| 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 |
+--------------------+--------------+------+-----+---------+
Axle 1
+--------------------+--------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+--------------------+--------------+------+-----+---------+
| id | int(11) | NO | Pk | NULL |
| vehicle_id | varchar(35) | NO | FK | NULL |
| weight | varchar(35) | NO | | NULL |
+--------------------+--------------+------+-----+---------+
Axle 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的所有字段。 vehicle_id是3个表(info,axle1,axis 2)中每个表中对Vehicle表中主键的引用。有人可以解释一下我可能会这样做吗?我尝试使用多个连接,但它没有用!非常感谢。
编辑:
我试过的查询是;
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
答案 0 :(得分: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
答案 1 :(得分:0)
检查此链接:mySQL get information from multiple tables in one 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