我有一张客户表和购买表, 需要在客户表中显示cname,cid和max(customer_visits) 和购买表中客户的total_purchases总和。 我正在做这样的事情
select p.cid, c.cname, sum(p.total_price)
from customers c where exists
(select max(visits_made) from customers having visits_made=max(visits_made)
and cid=p.cid)
inner join purchases p on p.cid=c.cid
group by p.cid,c.cname
和
select p.cid, c.cname, sum(p.total_price)
(select max(visits_made) from customers c where c.cid=p.cid)
from purchases p
inner join customers c on c.cid=p.cid
group by p.cid,c.cname
这些查询出了什么问题?
找到解决方案,必须在内连接后包含where子句:D
答案 0 :(得分:0)
我认为这只是一个聚合查询:
select p.cid, c.cname, sum(p.total_price) as total_price,
max(visits_made) as visits_made
from purchases p inner join
customers c
on c.cid = p.cid
group by p.cid, c.cname;