简体   繁体   中英

column “d.discount_amount” must appear in the GROUP BY clause or be used in an aggregate function

I have written this query to find the payment using two different table ,

SELECT (SUM(o_p.price) - d.discount_amount) as payment
FROM order_products as o_p
LEFT JOIN discounts as d  ON O_p.order_id = d.order_id
GROUP BY o_p.order_id

It shows an error column "d.discount_amount" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT o_p.order_id, (SUM(price) - d.discount_amount) as pay...

9:23 PM

Any suggestions, please?

select t1.sm - t2.discount_amount
  from (
select t1.order_id,
       sum(t1.price) sm
  from order_products t1
 order by t1.order_id) t1 left join discounts t2 on t1.order_id = t2.order_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM