日期:2014-05-16 浏览次数:20805 次
1. select s.gid,sum(s.num*g.price) as mm from sales s,goods g where s.gid=g.gid group by s.gid 2. select b.* from buyer b,sales ss where b.bid=ss.bid and b.bid not in( select bid from sales s where exists (select 1 from goods g where s.gid=g.gid and g.name='A') ) 3. select s.bid,b.name,sum(s.num*g.price) as mm from sales s,goods g,buyer b where s.gid=g.gid and b.bid=s.bid group by s.bid,b.name order by 3 limit 3
------解决方案--------------------
贴错了。。。重新贴一次
1. --如果销售记录在sales为空则total = 0 select a.gid,a.name,sum(ifnull(a.price*b.num,0)) as total from goods a left join sales b on a.gid = b.gid group by a.gid
------解决方案--------------------
SELECT a.gid,(a.price*b.total) AS total FROM goods a LEFT JOIN (SELECT gid,SUM(num) AS total FROM sales GROUP BY gid) b ON a.gid=b.gid