Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用SQL中的其他表计算每一行?_Mysql_Sql - Fatal编程技术网

Mysql 如何使用SQL中的其他表计算每一行?

Mysql 如何使用SQL中的其他表计算每一行?,mysql,sql,Mysql,Sql,我有两张桌子:书桌和作者桌。我需要计算每个作者在图书表中注册的图书数量。怎么做 SELECT *, (SELECT Count(dbo.Book.ID) FROM dbo.Book WHERE dbo.Book.AuthorID = dbo.Author.ID) AS NumberOfBooks FROM dbo.Author 您必须用一个ID连接两个表,以便找出两个表之间的关系。sql语句中有一个名为count的group函数,您可以

我有两张桌子:书桌和作者桌。我需要计算每个作者在图书表中注册的图书数量。怎么做

SELECT *,
       (SELECT Count(dbo.Book.ID)
        FROM   dbo.Book
        WHERE  dbo.Book.AuthorID = dbo.Author.ID) AS NumberOfBooks
FROM   dbo.Author 

您必须用一个ID连接两个表,以便找出两个表之间的关系。

sql语句中有一个名为count的group函数,您可以在其中添加group 比如说

SELECT author_name
       Count(books.id)
FROM   author,
       book
WHERE  book.author_id = author_id 
GROUP BY AUTHOR

我想这会对你有所帮助。我已经练习过了。你的问题是SQLFIDDLE

以下是我使用的SQL模式:

create table book(  
  book_id int,
  author_name varchar(100),
  book_name varchar(100)
);
create table author(  
 author_id int,
 author_name varchar(100)
);
insert into author values(1, 'shovon');
insert into author values(11, 'arsho');
insert into author values(111, 'soda');
insert into author values(1111, 'nfs');
insert into book values(1, 'shovon', 'book shovon 1');
insert into book values(2, 'soda', 'book soda 1');
insert into book values(3, 'shovon', 'book shovon 2');
insert into book values(4, 'arsho', 'book arsho 1');
insert into book values(5, 'ar', 'book ar 1');
select a.author_name, count(b.book_name) from 
(author as a 
  left join book as b on 
  a.author_name=b.author_name) 
group by a.author_name
order by count(b.book_name) desc;
以下是我使用的查询:

create table book(  
  book_id int,
  author_name varchar(100),
  book_name varchar(100)
);
create table author(  
 author_id int,
 author_name varchar(100)
);
insert into author values(1, 'shovon');
insert into author values(11, 'arsho');
insert into author values(111, 'soda');
insert into author values(1111, 'nfs');
insert into book values(1, 'shovon', 'book shovon 1');
insert into book values(2, 'soda', 'book soda 1');
insert into book values(3, 'shovon', 'book shovon 2');
insert into book values(4, 'arsho', 'book arsho 1');
insert into book values(5, 'ar', 'book ar 1');
select a.author_name, count(b.book_name) from 
(author as a 
  left join book as b on 
  a.author_name=b.author_name) 
group by a.author_name
order by count(b.book_name) desc;

SQLFIDDLE的Url

为我们提供您的表的方案选择author,count*from book group by author您确定您只有2个表吗?如果一本书有多个作者会怎样?顺便说一句,熟悉分组和计数。你至少应该自己试试,告诉我们你哪里有问题。一段带有表的模式的代码也很好,这是过于复杂和低效的。此外,这遵循ms sql语法,而不是mysql。那么您有什么建议?您能提出更有效的方法吗?如果您使用视图进行这种类型的报告,效果会更好。@m0riartis我已经在一篇评论中告诉过您:使用group by和count.EDIT选择Countdbo.Book.ID,dbo.Book内部的dbo.AUTHER.Name加入dbo.Book上的dbo.AuthorID=dbo.AUTHER.ID group by dbo.AUTHER.Name这将返回单个作者的结果,不是所有的。