0

これが私の最新のエンドバーです..サブクエリの結果を追加して別のフィールドまたは合計を取得しようとしています...しかし、この単純な構文が機能していないようです..誰かが私を正しい方向に向けてください...前もって感謝します

    SELECT 
    sub_events.name AS sub_event, 
    clients.name AS client, 
    divisions.name AS division, 
    subdivisions.name AS subdivision, 
    (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 itinerary_booking_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS flight, 
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 1 and itinerary_booking_id = 2 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS flight_change, 
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND itinerary_booking_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS hotel, 
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 and itinerary_booking_id = 3 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS hotel_change, 
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3  AND itinerary_booking_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL)  AS transfer, 
    (SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND itinerary_booking_id = 4 AND booking_id = bookings.id AND client_cost IS NOT NULL) AS transfer_change
    (SELECT SUM((SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND itinerary_booking_id = 4 AND booking_id = bookings.id AND client_cost IS NOT NULL)
            +(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 1 and itinerary_booking_id = 2 AND booking_id = bookings.id AND client_cost IS NOT NULL)
            +(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 AND itinerary_booking_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL)
            +(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 2 and itinerary_booking_id = 3 AND booking_id = bookings.id AND client_cost IS NOT NULL)
            +(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3  AND itinerary_booking_id = 1 AND booking_id = bookings.id AND client_cost IS NOT NULL)
            +(SELECT client_cost FROM itineraries WHERE itinerary_type_id = 3 AND itinerary_booking_id = 4 AND booking_id = bookings.id AND client_cost IS NOT NULL)) AS Total)
FROM 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 
GROUP BY bookings.id`
4

1 に答える 1

0

あなたはしたくないSUM()、あなたはそれらを追加したいだけです:

... AS transfer_change,
(SELECT client_cost FROM itineraries ...) +
(SELECT client_cost FROM itineraries ...) +
... +
(SELECT client_cost FROM itineraries ...) AS Total


ただし、これは恐ろしいクエリであり、相互に関連するサブクエリが多数あるため、がらくたのように実行されます。旅程への単純な結合とorステートメントsum()に基づくいくつかの値で、これらすべてを実現できます。ifcase

于 2012-10-26T02:21:05.740 に答える