Mysql中的多连接表仅返回一行[重复]
Posted
技术标签:
【中文标题】Mysql中的多连接表仅返回一行[重复]【英文标题】:Multi Join Table in Mysql returning only one row [duplicate] 【发布时间】:2021-12-05 21:19:23 【问题描述】:您好,我正在就我的代码寻求帮助,目前它只返回一行,我完全被这一行困住了,似乎无法修复它。我希望其他人的见解可以帮助我。
use library;
#3
Select title, count(loanId) as 'Number of loans'
From book
Join bookCopy ON bookCopy.isbn = book.isbn
Join loan ON bookCopy.copyId = loan.copyId
Where dateBack IS NULL
Order By title;
【问题讨论】:
你忘了GROUP BY title
如果没有group by
,mysql 是 allowing you to get away with,则您的查询是无效的 SQL
【参考方案1】:
那是因为 COUNT。
使用 GROUP BY 标题
Select title, count(loanId) as 'Number of loans'
From book
Join bookCopy ON bookCopy.isbn = book.isbn
Join loan ON bookCopy.copyId = loan.copyId
Where dateBack IS NULL
GROUP BY title
Order By title;
【讨论】:
以上是关于Mysql中的多连接表仅返回一行[重复]的主要内容,如果未能解决你的问题,请参考以下文章
mysql连接具有相同列值的两行并计算某些列值并返回一行中的所有行