我有两个查询,我想合并以提供单个输出。 这是第一个查询: -
SELECT `booking_request`.`booking_id`,`booking_type`,`from_date`,`to_date`,`city`,`pickUp_point`,`drop_point`,`reporting_time` FROM `booking_request`
JOIN `tour_chart` ON
`booking_request`.`booking_id` = `tour_chart`.`booking_id`
WHERE `tour_chart`.`driver_id` = '2';
这是第二个查询: -
SELECT `guest_name`,`guest_mobile` FROM `guest_info`
JOIN `guest_booking_table` ON
`guest_info`.`guest_id` = `guest_booking_table`.`guest_id`
JOIN `tour_chart` ON
`guest_booking_table`.`booking_id` = `tour_chart`.`booking_id`
WHERE `tour_chart`.`driver_id` = '$driver_id';
我尝试过这样的事情: -
SELECT * FROM
(SELECT `tour_chart`.`driver_id`,`booking_request`.`booking_id`,`booking_type`,`from_date`,`to_date`,`city`,`pickUp_point`,`drop_point`,`reporting_time` FROM `booking_request`
JOIN `tour_chart` ON
`booking_request`.`booking_id` = `tour_chart`.`booking_id`
WHERE `tour_chart`.`driver_id` = '2') x INNER JOIN
(SELECT `tour_chart`.`driver_id`,`guest_name`,`guest_mobile` FROM `guest_info`
JOIN `guest_booking_table` ON
`guest_info`.`guest_id` = `guest_booking_table`.`guest_id`
JOIN `tour_chart` ON
`guest_booking_table`.`booking_id` = `tour_chart`.`booking_id`
WHERE `tour_chart`.`driver_id` = '2') y
ON x.`tour_chart`.`driver_id` = y.`tour_chart`.`driver_id`;
如何将这两个查询合并为一个查询,该查询提供的结果包含booking_id,booking_type,from_date,to_date,city,pickUp_point,drop_point,reporting_time,guest_name和guest_mobile。 任何帮助或建议表示赞赏。 谢谢。
答案 0 :(得分:2)
我认为您可以将所有join
组合成一个查询:
SELECT br.booking_id, bt.booking_type,
from_date, to_date, city,
pickUp_point, drop_point, reporting_time,
gi.guest_name, gi.guest_mobile
FROM booking_request br JOIN
tour_chart tc
ON br.booking_id = tc.booking_id JOIN
guest_booking_table gbt
ON gbt.booking_id = tc.booking_id JOIN
guest_info gi
ON gi.guest_id = gbt.guest_id
WHERE tc.driver_id = 2;
注意:
driver_id
是一个数字,所以我删除了单引号。答案 1 :(得分:1)
请在加入on x.driver_id = y.driver_id;
时尝试使用此更正版本,因为您正在创建不再是x
的“临时”y
和tour_chart
表,只是列的名称。
select driver_id, booking_id, booking_type, from_date, to_date, city, pickup_point, drop_point, reporting_time, guest_name, guest_mobile
from (select tour_chart . driver_id
,booking_request . booking_id
,booking_type
,from_date
,to_date
,city
,pickup_point
,drop_point
,reporting_time
from booking_request
join tour_chart
on booking_request . booking_id = tour_chart . booking_id
where tour_chart . driver_id = '2') x
inner join (select tour_chart . driver_id
,guest_name
,guest_mobile
from guest_info
join guest_booking_table
on guest_info . guest_id = guest_booking_table . guest_id
join tour_chart
on guest_booking_table . booking_id = tour_chart . booking_id
where tour_chart . driver_id = '2') y
on x.driver_id = y.driver_id;