MySQL AS语句生成错误

时间:2014-07-11 21:39:11

标签: mysql

我正在阅读Murach MySQL的书,我不能从第3章开始练习其中一个,我不知道出了什么问题。 discount_amount列导致:

ERROR 1054 (42S22): Unknown column 'list_price' in 'field list'

那就是我使用AS的原因。有没有人看到我的代码有什么问题?

SELECT product_name,
   list_price,
   discount_percent,
   list_price*discount_percent AS discount_amount,
   list_price-discount_amount AS discount_price,
   ROUND(discount_amount, 2),
   ROUND(discount_price, 2)
FROM products
ORDER BY discount_price DESC
LIMIT 5;

1 个答案:

答案 0 :(得分:1)

需要在ROUND

中再次进行计算
SELECT product_name,
   list_price,
   discount_percent,
   list_price*discount_percent AS discount_amount,
   list_price-discount_amount AS discount_price,
   ROUND(list_price*discount_percent, 2),
   ROUND(list_price-discount_amount, 2)
FROM products
ORDER BY discount_price DESC
LIMIT 5;