2

我有 4 个表,我试图在一个表中提取某些数据,这个查询可能结构不太好,但我的原型至少需要一些东西。我有

    用户
    ------
    ID
    parent_id
    姓名
    姓

    预订
    --------
    ID
    client_id
    Division_id
    细分_id
    sub_event_id
    host_id
    客人ID

    行程
    ----------
    ID
    预订编号
    itinerary_type_id
    客户成本

    行程类型
    -------------
    航班
    酒店
    转移

这是我当前的查询

    

         选择

         (SELECT CONCAT(name,' ',surname) FROM users WHERE id = bookings.host_id) 作为主机,
    CONCAT(users.name,' ',users.surname) AS 名称,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 1 AND booking_id = bookings.id AND client_cost 不为空)作为航班,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND booking_id = bookings.id AND client_cost 不为空)作为酒店,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND booking_id = bookings.id AND client_cost 不为空)作为转移,
    (总计 1 + 总计 2 + 总计 3)AS 总计

    从

    (SELECT(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 1 AND booking_id = bookings.id AND client_cost 不为空)作为total1,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND booking_id = bookings.id AND client_cost 不为空) 作为total2,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND booking_id = bookings.id AND client_cost 不为空)作为total3 FROM bookings)q,用户

    在 bookings.guest_id = users.id 上加入预订
    在 users.client_id = bookings.client_id 上加入客户
    在 details.user_id = users.id 上加入详细信息
    在 Divisions.client_id = users.client_id 上加入部门
    加入细分 ON subdivisions.division_id = bookings.division_id 加入 sub_events ON sub_events.id = bookings.sub_event_id
    在 itineraries.booking_id = bookings.id WHERE bookings.id = 1572 上加入行程
    GROUP BY bookings.id

预期的结果是

    主持人|姓名|航班|酒店|接送|总计
    --------------------------------------------------
    迈克·利特尔|鲍勃·帕克|200 |100 |30 |330

目前我正在使用子查询来临时创建一个表格,从中获取总数..但它似乎不起作用......我认为,问题是我无法将预订 ID 传递给新的临时子查询表....还有其他解决方法吗?我将永远伟大!:-) .. 真的 !!!

4

1 回答 1

2

这可能不是您正在寻找的确切 SQL(省略了很多与其他代码表的连接,但仅包括主要查询)并且可能存在愚蠢的语法错误,因为我没有对其进行测试。但想法是为预订选择一个摘要,然后将摘要与用户表连接起来。(查询被推广以适应多个预订 ID 选择。)

SELECT
    booking_summary.booking_id,
    booking_summary.host_id,
    concat(users.name, ' ', users.surname) as host, 
    concat(users.name, ' ', users.surname) as name, 
    booking_summary.flight, 
    booking_summary.hotel, 
    booking_summary.transfer, 
    (booking_summary.flight+booking_summary.hotel+booking_summary.transfer) as total
FROM
    (select booking_id, max(host_id) as host_id, sum(flight) as flight, sum(hotel) as hotel, sum(transfer) as transfer from (
        select b.host_id, i.booking_id, i.client_cost as flight, 0 as hotel, 0 as transfer from itineraries i, bookings b where i.itinerary_type_id = 1 AND i.booking_id in (1572, 1573, 1574) and i.booking_id=b.id
        union all
        select b.host_id, i.booking_id, 0 as flight, i.client_cost as hotel, 0 as transfer from itineraries i, bookings b where i.itinerary_type_id = 2 AND i.booking_id in (1572, 1573, 1574) and i.booking_id=b.id
        union all
        select b.host_id, i.booking_id, 0 as flight, 0 as hotel, i.client_cost as transfer from itineraries i, bookings b where i.itinerary_type_id = 3 AND i.booking_id in (1572, 1573, 1574) and i.booking_id=b.id
    ) temp_booking_result group by booking_id) booking_summary,
    users
WHERE
    users.id=booking_summary.host_id;
于 2012-10-25T04:14:19.573 回答