2

4 つのテーブルがあり、特定のデータを 1 つに抽出しようとしています。このクエリはあまり構造化されていない可能性がありますが、プロトタイプには少なくとも何かが必要です。私は得た

    ユーザー
    ------
    ID
    親ID
    名前
    姓

    予約
    --------
    ID
    クライアントID
    Division_id
    subdivision_id
    sub_event_id
    host_id
    ゲスト_id

    旅程
    ----------
    ID
    予約ID
    itinerary_type_id
    client_cost

    itinerary_type
    -------------
    フライト
    ホテル
    移行

これが私の現在のクエリです

    

         選択する

         (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 IS NOT NULL) AS フライト、
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND booking_id = bookings.id AND client_cost IS NOT NULL) ホテルとして、
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS transfer,
    (合計 1 + 合計 2 + 合計 3) AS 合計

    から

    (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

    bookings.guest_id = users.id で予約に参加
    users.client_id = bookings.client_id でクライアントに参加
    JOIN 詳細 ON details.user_id = users.id
    JOIN 部門 ON部門.client_id = users.client_id
    subdivisions.division_id = bookings.division_id で subdivisions に参加 sub_events.id = bookings.sub_event_id で sub_events に参加
    itineraries.booking_id = bookings.id WHERE bookings.id = 1572 で旅程に参加
    GROUP BY bookings.id

期待される結果は

    ホスト|名前|フライト|ホテル|転送|合計
    --------------------------------------------------
    マイク・リトル|ボブ・パーカー|200 |100 |30 |330

現在、サブクエリを使用して、合計を取得している場所からテーブルを一時的に作成しています..しかし、うまくいかないようです...問題は、予約IDを新しい一時サブクエリに渡すことができないことです.テーブル....それを回避する方法は他にありますか? 私は永遠に素晴らしいです!:-) .. 本当 !!!

4

1 に答える 1

2

This may not be the exact SQL you are looking for (omitted a lot of joins to other code tables but included only the major query) and there could be silly syntax errors as I have not tested it. But the idea is to select a summary for the bookings, then join the summary with user table. (The query is generalized to accommodate multiple booking ID selection.)

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 に答える