如何在SELECT中使用别名

时间:2013-08-07 03:39:16

标签: mysql

我在下面的mysql语句一切正常,直到我添加(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`

1 个答案:

答案 0 :(得分: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中创建一个计算列吗?