但是我想给它分组,汇总起来,想用gruop by来分组同时在子查询外面加个sum函数,但是报错
然后我又想到可不可以用子查询里的where一样
还是报错
cust_id要在分组字段中。
条件有问题,子查询会返回多笔记录,导致无法运行。
select p.cust_id,p.cust_name,t.allnum,allprice from product as p left join (select cust_id,count(oi.order_num) as allnum,sum(oi.quantitle*oi.item_price) as allprice from orders as o left join orderitems as oi on o.order_num=oi.order_num group by cust_id ) as t on p.cust_id=t.cust_id
这个改试试 其中 product 是你第一个产品表,orders是订单表,orderitems是记录表