gpt4 book ai didi

MySQL SUM、INNER JOIN 和 GROUP BY 在同一条语句中。语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:31 25 4
gpt4 key购买 nike

我需要从这四个表中获取每个艺术家的总销售额:

Artists (artist.id, artist.name)
Events (events.id, events.title, events.artist_id)
Sales (sales.events_id, sales.buyer_id, sales.amount)
Buyer (buyer.id, buyer.name)

我需要以下语句连接三个表:

SELECT sales.amount as SalesTotal, artists.title AS Artist
SUM (SalesTotal)
FROM sales
INNER JOIN events ON events.id = sales.event_id
INNER JOIN artists ON artists.id = events.artist_id
GROUP BY Artist

但我总是遇到语法错误。我用谷歌搜索了它,但我得到的所有示例都在同一张表中。我不知道错误应该在哪里。任何帮助将不胜感激

最佳答案

我想你想要这个:

SELECT 
SUM(sales.amount) as SalesTotal,
artists.title AS Artist
FROM sales
INNER JOIN events ON events.id = sales.event_id
INNER JOIN artists ON artists.id = events.artist_id
GROUP BY artists.title;

您不能在 group by 子句中使用别名,因为语句中不同子句的评估或执行顺序不同。 group by 子句在应用列列表中的别名定义之前执行。

关于MySQL SUM、INNER JOIN 和 GROUP BY 在同一条语句中。语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com