为什么我在SQL上不断收到相同的错误消息

时间:2019-03-08 07:03:33

标签: mysql sql

概述:写一个SELECT语句来总结吉他店的订单

GROUP BY order_id
HAVING MAX(discount_amount)>500
ORDER BY order_id ASC

我不断收到此错误消息:错误代码1055。选择列表的表达式#3不是

3 个答案:

答案 0 :(得分:0)

在使用聚合函数时使用sum(quantity),还必须以聚合方式使用它,否则需要在group by子句中添加

SELECT order_id, COUNT(*) AS num_items, SUM(item_price - discount_amount) * 
sum(quantity) AS order_total, MAX(discount_amount) AS max_item_discount
FROM order_items
GROUP BY order_id
HAVING MAX(discount_amount)>500
ORDER BY order_id ASC

答案 1 :(得分:0)

按组将数量用作引擎ONLY_FULL_GROUP_BY

   SELECT order_id, COUNT(*) AS num_items, SUM(item_price - discount_amount) * 
    quantity AS order_total, MAX(discount_amount) AS max_item_discount
    FROM order_items
    GROUP BY order_id,quantity
    HAVING MAX(discount_amount)>500
    ORDER BY order_id ASC

否则,请在聚合quantity内使用sum((item_price - discount_amount) * quantity)

答案 2 :(得分:0)

您的列数量未按螺母分组,可能是因为您需要将列移动到item_price-Discount_amount的内

    SELECT order_id
        , COUNT(*) AS num_items
        , SUM((item_price - discount_amount) * quantity ) AS order_total
        , MAX(discount_amount) AS max_item_discount
    FROM order_items
    GROUP BY order_id
    HAVING MAX(discount_amount)>500
    ORDER BY order_id ASC