我是一个SQL新手,试图编写一个涉及多个表联接的select查询,多次使用一个表。
我想要实现的查询的输出是:
Truck_ID|Pushbutton_ID|Trailer01_ID|Trailer02_ID|Trailer03_ID|Trailer04_ID|Trailer05_ID
表格Trucks
和Pushbutton_Assignments
的加入非常简单。难点是多次加入Trailer_Assignment
表。
Trailer_Assignments
的结构是:
Trailer_ID|Truck_ID|Order
Trailer_ID
和Truck_ID
字段与其他表具有外键关系。查询的TrailerXX_ID
字段与Order
表的Trailer_Assignment
字段相关;所有Order == 1
的记录都有Trailer_ID
Trailer01_ID
,Order == 2
到Trailer02_ID
的记录等等。
我基于here的查询。它适用于单个列,但Access(我认为)不像后续列。
工作查询代码(单列):
SELECT Trucks.ID,
Pushbutton_Assignment.Pushbutton_ID,
Trailer_Assignment.Trailer_ID AS Trailer01_ID,
Trailer_Assignment.Trailer_ID AS Trailer02_ID,
Trailer_Assignment.Trailer_ID AS Trailer03_ID,
Trailer_Assignment.Trailer_ID AS Trailer04_ID,
Trailer_Assignment.Trailer_ID AS Trailer05_ID
FROM (Trucks LEFT JOIN Pushbutton_Assignment ON Trucks.ID = Pushbutton_Assignment.Truck_ID) LEFT JOIN Trailer_Assignment ON Trucks.ID = Trailer_Assignment.Truck_ID AND Trailer_Assignment.Order = 1;
编辑:
非工作查询代码(两列):
SELECT Trucks.ID,
Pushbutton_Assignment.Pushbutton_ID,
Trailer_Assignment.Trailer_ID AS Trailer01_ID,
Trailer_Assignment.Trailer_ID AS Trailer02_ID,
Trailer_Assignment.Trailer_ID AS Trailer03_ID,
Trailer_Assignment.Trailer_ID AS Trailer04_ID,
Trailer_Assignment.Trailer_ID AS Trailer05_ID
(((FROM (Trucks LEFT JOIN Pushbutton_Assignment ON Trucks.ID = Pushbutton_Assignment.Truck_ID))
LEFT JOIN Trailer_Assignment ON (Trucks.ID = Trailer_Assignment.Truck_ID AND Trailer_Assignment.Order = 1))
LEFT JOIN Trailer_Assignment ON (Trucks.ID = Trailer_Assignment.Truck_ID AND Trailer_Assignment.Order = 2));
这是使用Access 2003。
答案 0 :(得分:1)
MS Access具有各种奇怪的限制,例如在LEFT JOIN
条件下难以处理常量。所以,只需投降并使用相关的子查询。 。 。你只需要一个值:
select t.id, pba.pushbutton_id,
(select top 1 ta.trailer_id from Trailer_Assignment as ta where ta.Truck_id = t.id and ta.order = 1
) as Trailer01_id,
(select top 1 ta.trailer_id from Trailer_Assignment as ta where ta.Truck_id = t.id and ta.order = 2
) as Trailer02_id,
(select top 1 ta.trailer_id from Trailer_Assignment as ta where ta.Truck_id = t.id and ta.order = 3
) as Trailer03_id,
(select top 1 ta.trailer_id from Trailer_Assignment as ta where ta.Truck_id = t.id and ta.order = 4
) as Trailer04_id,
(select top 1 ta.trailer_id from Trailer_Assignment as ta where ta.Truck_id = t.id and ta.order = 5
) as Trailer05_id
from Trucks as t LEFT JOIN
Pushbutton_Assignment as pba
on t.id = pba.truck_id;
答案 1 :(得分:0)
由于您在查询中多次使用同一个表,因此需要为每个表提供一个别名:
SELECT Trucks.ID,
Pushbutton_Assignment.Pushbutton_ID,
ta1.Trailer_ID AS Trailer01_ID,
ta2.Trailer_ID AS Trailer02_ID,
ta3.Trailer_ID AS Trailer03_ID,
ta4.Trailer_ID AS Trailer04_ID,
ta5.Trailer_ID AS Trailer05_ID
FROM (Trucks LEFT JOIN Pushbutton_Assignment ON Trucks.ID = Pushbutton_Assignment.Truck_ID)
LEFT JOIN Trailer_Assignment ta1 ON Trucks.ID = ta1.Truck_ID AND ta1.Order = 1
LEFT JOIN Trailer_Assignment ta2 ON Trucks.ID = ta2.Truck_ID AND ta2.Order = 2
LEFT JOIN Trailer_Assignment ta3 ON Trucks.ID = ta3.Truck_ID AND ta3.Order = 3
LEFT JOIN Trailer_Assignment ta4 ON Trucks.ID = ta4.Truck_ID AND ta4.Order = 4
LEFT JOIN Trailer_Assignment ta5 ON Trucks.ID = ta5.Truck_ID AND ta5.Order = 5;
答案 2 :(得分:0)
SELECT Trucks.ID,
Pushbutton_Assignment.Pushbutton_ID,
iif(ta.order = 1, ta.Trailer_ID, NULL) AS Trailer01_ID,
iif(ta.order = 2, ta.Trailer_ID, NULL) AS Trailer02_ID,
iif(ta.order = 3, ta.Trailer_ID, NULL) AS Trailer03_ID,
iif(ta.order = 4, ta.Trailer_ID, NULL) AS Trailer04_ID,
iif(ta.order = 5, ta.Trailer_ID, NULL) AS Trailer05_ID
FROM Trucks LEFT JOIN Pushbutton_Assignment
ON Trucks.ID = Pushbutton_Assignment.Truck_ID
LEFT JOIN Trailer_Assignment ta ON Trucks.ID = ta.Truck_ID
这是使用if
语句执行此操作的另一种方法。您只需要参考一次表格。如果需要,添加where
子句,仅查找订单1,2,3,4和5。