如何加入两个单独的MySQL查询来给出一个结果?

时间:2016-10-31 10:21:54

标签: mysql sql

我有两个查询,我想合并以提供单个输出。 这是第一个查询: -

    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。 任何帮助或建议表示赞赏。 谢谢。

2 个答案:

答案 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的“临时”ytour_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;