我试图从 3 个表中获取数据,即:“艺术家”、“销售”、“索赔”所以我想尝试获得每个“艺术家”的收入(来自总“销售额”),如果他们已经声明了一些销售额,这是保存在“声明”表中的数据这是每个表的结构:*Table Artist:Artist_idArtist_nameArtist_profile*Table Sales :Sales_idArtist_nameSales_amount*Tables Claim :Claim_idArtist_nameClaim_amount我尝试了一些查询,但没有得到我期望的值在列“Claim_amount”中循环,假设表“sales”有几个数据,表“claim”还没有数据,因为没有索赔历史.SELECT artist.member_id,artist.profile_pict, sales.artist_name, SUM(sales.amount) AS total_sales ,claim.claim_amountFROM sales,artist,claimWHERE sales.artist_name OR claim.artist_name = artist.username GROUP BY sales.artist_name ORDER BY total_sales DESC 我希望输出类似于示例:Artis_id | Artist_profile | Artist_name | Total_amount | Total_claim1 Artist A My Artist 1 100 502 Artist B My Artist 2 200 null (because there is no history)3 Artist C My Artist 3 300 150
2 回答
万千封印
TA贡献1891条经验 获得超3个赞
用 left join
SELECT a.member_id,a.profile_pict, a.artist_name,
SUM(sales.amount) AS total_sales ,sum(claim_amount) as total_claim
FROM artist a left join sales s on a.Artist_name=s.Artist_name
left join claim c on c.Artist_name= a.Artist_name
GROUP BY a.member_id,a.profile_pict, a.artist_name
ORDER BY total_sales DESC
注意:最好使用显式连接而不是逗号分隔连接
- 2 回答
- 0 关注
- 160 浏览
添加回答
举报
0/150
提交
取消