子查询再次加入和完全困境

时间:2012-10-26 00:06:06

标签: mysql join sum

所以这是我的最新尝试..尝试添加子查询的结果以获得另一个字段或总计...但总结这个简单语法似乎不起作用..可以有人请指出正确的方向......提前谢谢

    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`

1 个答案:

答案 0 :(得分:0)

您不想SUM(),只想添加它们:

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


然而,这是一个可怕的查询,并且由于大量的核心子查询而会像垃圾一样执行。您可以通过简单的行程连接和基于sum()if语句的一些case值来实现这一切。