SQL多表链接查询两个表a、b
发布网友
发布时间:2022-05-30 06:04
我来回答
共5个回答
热心网友
时间:2023-10-11 22:58
【有疑问可以HI我,或追问,但请不要关闭问题,谢谢!】
select a.*,sum(b.amount) from a,b
where b.custid=a.custid and (b.date between a.sdate and a.edate)追问提示a.custid 等其他字段在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句
要不麻烦加我qq吧 5931779 。在线等,谢谢了!
追答select custid,sdate,edate,sum(total)
from
(
select a.custid,a.sdate,a.edate,0 as total from a
union(
select a.custid,a.sdate,a.edate,sum(b.amount)as total
from a,b where a.custid=b.custid and b.date between a.sdate and a.edate
group by a.custid,a.sdate,a.edate)
)result
group by custid,sdate,edate
热心网友
时间:2023-10-11 22:58
select a.*,sum(amont) as sum_amout
from a join b on b.custid=a.custid
where b.date between a.sdate and a.edate
group by a.custid,a.sdate,a.edate,a.amount
热心网友
时间:2023-10-11 22:59
select a.*,sum(b.amount) as sum_amount
from a join b on b.custid=a.custid
where b.date between a.sdate and a.edate
group by a.custid,a.sdate,a.edate,a.amount
feixianxxx 的回答已经很正确了!
热心网友
时间:2023-10-11 22:59
select a.custid,sdate,edate,amount,
(select sum(amountb) from b where b.custid=a.custid and (b.date between a.sdate and a.edate)) as 'xxx'
from a inner join b on a.custid=b.custid
热心网友
时间:2023-10-11 23:00
你这表述有问题啊!
不知道下面的语句是不是您想要的结果
select a.custid,a.sdate,a.edate,a.amount,sum(b.amount) from a inner join b on b.custid=a.custid where b.date between a.sdate and a.edate group by a.custid,a.sdate,a.edate,a.amount;