Mysql加入查询困境

时间:2012-10-25 03:50:51

标签: mysql subquery

我有4个表,我试图在一个中提取某些数据,这个查询可能不是很好的结构,但我需要至少为我的原型。我得到了

    users
    ------
    id
    parent_id
    name
    surname

    bookings
    --------
    id
    client_id
    division_id
    subdivision_id
    sub_event_id
    host_id
    guest_id

    itineraries
    ----------
    id
    booking_id
    itinerary_type_id
    client_cost

    itinerary_type
    -------------
    flight
    hotel
    transfer

这是我当前的查询

    

         SELECT 

         (SELECT CONCAT(name,' ',surname) FROM users WHERE id = bookings.host_id) AS host, 
    CONCAT(users.name,'  ',users.surname) AS name, 
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS flight,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS hotel,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND booking_id = bookings.id AND client_cost IS NOT NULL)  AS transfer,
    (total1 + total2 + total3)  AS total

    FROM 

    (SELECT(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL) as total1,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND booking_id = bookings.id AND client_cost IS NOT NULL) as total2,
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND booking_id = bookings.id AND client_cost IS NOT NULL) as total3 FROM  bookings) q, users 

    JOIN bookings ON bookings.guest_id = users.id 
    JOIN clients ON users.client_id = bookings.client_id 
    JOIN details ON details.user_id = users.id 
    JOIN divisions ON divisions.client_id = users.client_id 
    JOIN subdivisions ON subdivisions.division_id = bookings.division_id JOIN sub_events ON sub_events.id = bookings.sub_event_id 
    JOIN itineraries ON itineraries.booking_id = bookings.id WHERE bookings.id = 1572 
    GROUP BY bookings.id

预期结果是


    host       |name      |flight|hotel|transfer|Total
    --------------------------------------------------
    Mike Little|Bob Parker|200   |100  |30      |330

目前我正在使用子查询临时制作一张桌子,我从哪里得到总数..但它似乎没有用......我想,问题是我无法将预订ID传递给新的临时子查询表....还有更多的解决方法吗?我将永远伟大! :-) ..真的!!!

1 个答案:

答案 0 :(得分: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;