0

在我添加(sub_total - discount_total) AS total,.

我猜这是因为我正在调用尚未定义的别名(sub_totaldiscount_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`
4

1 回答 1

1

对于你的问题,我会这样解决:

SELECT *,     
(sub_total - discount_total) AS total
FROM (
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
FROM `bookings`
WHERE `bookings`.`status` = 'Quote'
) AS src
ORDER BY  src.`id` desc
LIMIT 0, 10

但这仅适用于您绝对要避免为您的列重新输入整个公式的情况。

您是否还考虑在 mysql 中创建计算列?

于 2013-08-07T03:51:35.740 回答