gpt4 book ai didi

mysql - 对列进行计数并内连接另一个表时出错

转载 作者:行者123 更新时间:2023-11-29 08:07:23 25 4
gpt4 key购买 nike

我正在尝试统计评论最多的新闻。我有两个表:评论和新闻,包含同一列:News_ID。这是查询

select *,count(News_ID) as count from comments inner join news where comments.News_ID = news.News_ID group by comments.News_ID order by count DESC limit 4

我收到此错误

#1052 - Column 'News_ID' in field list is ambiguous

这是否意味着我要计算的列?

最佳答案

您需要告诉您在选择中使用其列的表的名称有两个 News_ID 列,因此在计数中它对于查询选择哪个表的列是不明确的,也使用正确的语法进行连接on 子句

select n.*,c.*,count(n.News_ID) as count 
from comments c
inner join news n ON(c.News_ID = n.News_ID)
group by c.News_ID
order by count DESC limit 4

关于mysql - 对列进行计数并内连接另一个表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22433380/

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