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


然而,这是一个可怕的查询,并且由于大量相关的子查询而表现得像废话。您可以通过简单地加入行程和一些sum()基于iforcase语句的值来实现所有这些。

于 2012-10-26T02:21:05.740 回答