Mysql查询计算问题?

时间:2014-08-13 20:24:42

标签: mysql sql

我正在尝试以下查询来查找查询本身中的折扣值。

我曾尝试各种方法来弄清楚这里发生了什么,但我想我会去别的地方:

这是查询

SELECT
sales.quantity,
sales.sell_date,
items.itemname,
orders.orderunique,
orders.issold,
orders.revisionNumber,
orders.discountoffered,
categories.catName,
items.unitprice,
items.unitcost,
items.itemcode,
customer.customer,
orders.trackingref,
tbl_suppliers.suppliername,
items.unitprice-items.unitcost AS Profit,
((items.unitprice * orders.discountoffered/100) as discountedprice),
(items.unitprice-`discountedprice` AS discountcalculated),
(items.unitprice-`discountcalculated` AS DiscountedProfit) 
FROM
sales
INNER JOIN orders ON sales.orderID = orders.orderID
INNER JOIN items ON sales.itemID = items.itemID
INNER JOIN categories ON categories.catID = items.categoryID
INNER JOIN customer ON sales.customerID = customer.customerID
INNER JOIN tbl_suppliers ON tbl_suppliers.ID = items.supplierID
where sales.itemID = 3 and issold = 'yes'

这是我得到的错误:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as discountedprice),
(items.unitprice-`discountedprice` AS discountcalculated),' at line 17

2 个答案:

答案 0 :(得分:1)

您不能在查询的列列表中使用列别名:

Problems with Column Aliases

  

可以在查询选择列表中使用别名来为列a提供   不同的名字。您可以在GROUP BY,ORDER BY或HAVING中使用别名   条款引用专栏:

所以你必须重写这部分查询:

SELECT
    [....],
    ((items.unitprice * orders.discountoffered/100) as discountedprice),
    (items.unitprice-`discountedprice` AS discountcalculated),
    (items.unitprice-`discountcalculated` AS DiscountedProfit) 
FROM
    [...]

并写出表达式:

SELECT
    [....],
    (items.unitprice * orders.discountoffered/100) as discountedprice,
    items.unitprice - (items.unitprice * orders.discountoffered/100) AS discountcalculated,
    items.unitprice - (items.unitprice - (items.unitprice * orders.discountoffered/100)) AS DiscountedProfit) 
FROM
    [...]

但是您的DiscountedProfit出现了逻辑错误,因为您的折扣价格等于您的DiscountedProfit,请参阅:

items.unitprice - (items.unitprice - (items.unitprice * orders.discountoffered/100))

评估为

(items.unitprice * orders.discountoffered/100)

这是您对discountedprice列的定义。

答案 1 :(得分:0)

试试这个

SELECT
sales.quantity,
sales.sell_date,
items.itemname,
orders.orderunique,
orders.issold,
orders.revisionNumber,
orders.discountoffered,
categories.catName,
items.unitprice,
items.unitcost,
items.itemcode,
customer.customer,
orders.trackingref,
tbl_suppliers.suppliername,
items.unitprice-items.unitcost AS Profit,
((items.unitprice * orders.discountoffered)/100) as discountedprice,
(items.unitprice-`discountedprice` ) AS discountcalculated,
(items.unitprice-`discountcalculated` ) AS DiscountedProfit 
FROM ...

您正在为(.. AS discountcalculated))括号内的计算列提供别名,导致错误将其移至(col1 -col2 ) AS col_name

之外

此外,您不能在同一级别使用计算的别名,您必须重复整个计算表达式或使用子选择

SELECT
... ,
items.unitprice-items.unitcost AS Profit,
((items.unitprice * orders.discountoffered)/100) as discountedprice,
(items.unitprice - ((items.unitprice * orders.discountoffered)/100) ) AS discountcalculated,
(items.unitprice -(items.unitprice- ((items.unitprice * orders.discountoffered)/100) ) ) AS DiscountedProfit 
FROM ...