为什么我一直在SQL上得到相同的错误消息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我一直在SQL上得到相同的错误消息相关的知识,希望对你有一定的参考价值。
概述:编写一份SELECT语句,总结吉他店的订单
GROUP BY order_id
HAVING MAX(discount_amount)>500
ORDER BY order_id ASC
我不断收到此错误消息:错误代码1055.选择列表的表达式#3不是
答案
使用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
另一答案
使用数量作为您的引擎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)
另一答案
您有列数量不在分组中可能是您需要在item_price - discount_amount中移动te sum中的列
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
以上是关于为什么我一直在SQL上得到相同的错误消息的主要内容,如果未能解决你的问题,请参考以下文章