简体   繁体   English

MySQL添加联接子查询来计数

[英]Mysql adding join subquery to count

I've got two tables Loan and Book. 我有两张桌子的贷款和书本。

With this query I count most loaned books from Loan by counting Book_ID: 通过此查询,我通过计算Book_ID来计算从贷款中借出的大部分图书:

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

Now I would like to add Book title from Book table here. 现在,我想在这里从Book表中添加Book标题。 I have Book_ID as Foreign key in Loan table. 我有Book_ID作为贷款表中的外键。 I'm not quite sure how to put this query together. 我不太确定如何将这个查询放在一起。

This is what I tried but no results: 这是我尝试过的但没有结果:

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;

You have ambigious column names since you have joined to another table, try giving them an alias: 由于您已经加入了另一个表,所以您的列名含糊不清,请尝试为其赋予别名:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM