gpt4 book ai didi

Mysql加入join子查询计数

转载 作者:行者123 更新时间:2023-11-28 23:46:51 25 4
gpt4 key购买 nike

我有两个表 Loan 和 Book。

在这个查询中,我通过计算 Book_ID 来计算 Loan 中借出的图书数量:

select Book_ID, count(Book_ID) as Count
from Loan
group by Book_ID
order by Count desc;

现在我想在这里添加 Book 表中的书名。我在 Loan 表中将 Book_ID 作为外键。我不太确定如何将此查询放在一起。

这是我试过但没有结果:

select Book_ID, count(Book_ID) as Count, Book.Title
from Loan, Book
where Loan.Book_ID = Book.Book_ID
group by Book_ID
order by Count desc;

最佳答案

由于您已连接到另一个表,因此您的列名不明确,请尝试给它们起一个别名:

select Book.Book_ID, count(Book.Book_ID) as Count, Book.Title
from Loan, Book
where Loan.Book_ID = Book.Book_ID
group by Book.Book_ID
order by Count desc;

关于Mysql加入join子查询计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454427/

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