在我添加(sub_total - discount_total) AS total,
.
我猜这是因为我正在调用尚未定义的别名(sub_total
和discount_total
)。
如何定义别名以免引发错误#1054 - Unknown column 'sub_total' in 'field list'
SELECT SQL_CALC_FOUND_ROWS
`bookings`.`id`,
`bookings`.`client_id`,
`bookings`.`vehicle_id`,
`bookings`.`vehicle_hire`,
(insurance_driver_1 + insurance_driver_2 + insurance_driver_3) AS insurance_total,
`bookings`.`bedding_qty`,
`bookings`.`bedding_price`,
`bookings`.`promo_discount`,
`bookings`.`promo_is_percent`,
`bookings`.`promo_code`,
(vehicle_hire + insurance_driver_1 + insurance_driver_2 + insurance_driver_3) + (bedding_qty * bedding_price) AS sub_total,
CASE
WHEN promo_is_percent = 1
THEN (((vehicle_hire + insurance_driver_1 + insurance_driver_2 + insurance_driver_3) + (bedding_qty * bedding_price)) / 100) * promo_discount
WHEN promo_is_percent = 0
THEN promo_discount
END as discount_total,
(sub_total - discount_total) AS total
FROM `bookings`
WHERE `bookings`.`status` = 'Quote'
ORDER BY `bookings`.`id` desc
LIMIT 0, 10
谢谢
更新
下面的工作示例,
`SELECT SQL_CALC_FOUND_ROWS *,
(sub_total - discount_total) AS total
FROM (
SELECT
`bookings`.`id`,
`bookings`.`client_id`,
`bookings`.`vehicle_id`,
`bookings`.`vehicle_hire`,
(insurance_driver_1 + insurance_driver_2 + insurance_driver_3) AS insurance_total,
`bookings`.`bedding_qty`,
`bookings`.`bedding_price`,
`bookings`.`promo_discount`,
`bookings`.`promo_is_percent`,
`bookings`.`promo_code`,
(vehicle_hire + insurance_driver_1 + insurance_driver_2 + insurance_driver_3) + (bedding_qty * bedding_price) AS sub_total,
CASE
WHEN promo_is_percent = 1
THEN (((vehicle_hire + insurance_driver_1 + insurance_driver_2 + insurance_driver_3) + (bedding_qty * bedding_price)) / 100) * promo_discount
WHEN promo_is_percent = 0
THEN promo_discount
END as discount_total
FROM `bookings`
WHERE `bookings`.`status` = 'Quote'
) AS src
ORDER BY src.`id` desc
LIMIT 0, 10`