我试图使用连接查询从多个表中获取一些数据。我有一张预订表,用于存储"接送地点ID"和"删除位置ID"和另一张表" location"存储位置名称的位置。 我试图从"位置"中的2个不同变量中获取位置名称(提取和放置位置)。来自预订表的这两个ID表。
我的表格包含以下参数。 位置表:" locations_deatils"具有location_name location_id。 预订表:reservation_cars有pickup_location id,drop_location id,pick_up date,drop_date
SELECT l.location_name as pick-up_loc,
l.location_name as drop_loc,
c.pickup_date,
c.return_date,
FROM locations_deatils l inner join
reservation_cars c on l.locations_id = c.pickup_location and l.locations_id
= c.return_location
WHERE c.pickup_date >= :pickupTime and c.return_date <=:returnTime;
答案 0 :(得分:0)
也许你的意思是这样的:
SELECT p_loc.location_name AS pickup_location,
cars.pickup_date,
d_loc.location_name AS drop_location,
cars.return_date
FROM reservation_cars cars
JOIN location_details p_loc
ON cars.pickup_location = p_loc.location_id
JOIN location_details d_loc
ON cars.return_location = d_loc.location_id
WHERE .....
如果尚未设置取件和/或退货地点,您可能需要使用outer join
。
答案 1 :(得分:0)
您需要为接送和放置位置单独加入。
SELECT l1.location_name as 'pick-up location', l2.location_name as 'drop location', pickup_date, return_date
FROM reservation_cars r
JOIN locations_deatils l1 ON l1.locations_id = r.pickup_location
JOIN locations_deatils l2 ON l2.locations_id = r.return_location
WHERE r.pickup_date >= :pickupTime
AND r.drop_date <= :returnTime