所以这是我最新的尝试者 .. 尝试添加子查询的结果以获取另一个字段或总计...但是总而言之,这种简单的语法似乎不起作用.. 有人可以指出我正确的方向吗...提前致谢
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`