我写这篇文章是为了询问有关以下架构的查询的帮助:
想象一下有两种格式,例如PDF和电子书。我的目标是获得以下结果:
PushiblerName | Number_PDF | Numerber_BOOK |
--------------------------------------------------
ExampleName1 | 2 | 0
ExampleName2 | 3 | 1
有人会帮我写这个查询吗? 提前谢谢。
答案 0 :(得分:0)
select name,(select count(*) from t_book where IdPublisher = t_publisher.Id) as Numerber_BOOK,
(select count(*) from t_book where IdPublisher = t_publisher.Id and IdFormat = 3) as Number_PDF from t_publisher
用Pdf格式的Id替换3
答案 1 :(得分:0)
我想你可以尝试这样的事情
首先设置一些示例数据:
declare @t_book table (Id int, IdPublisher int)
declare @t_publisher table (Id int, Name varchar(50))
declare @t_book_format table (IdBook int, idFormat int)
declare @t_format table (Id int, Description varchar(50))
insert into @t_book values (1, 1), (2, 1), (3, 2), (4, 2), (5, 2), (6, 2)
insert into @t_publisher values (1, 'examplename1'), (2, 'examplename2')
insert into @t_book_format values (1, 1), (2, 1), (3, 2), (4, 1), (5, 1), (6, 1)
insert into @t_format values (1 ,'pdf'), (2, 'book')
现在查询:
select p.Name as PublisherName,
f.Description,
count(f.Id) as numbers
from @t_book b
inner join @t_publisher p on b.idPublisher = p.Id
inner join @t_book_format bf on b.Id = bf.idBook
inner join @t_format f on bf.idFormat = f.Id
group by p.Name, f.Description
但如果没有来自所有表的样本数据,这只是猜测
结果采用不同的格式,但如果在t_book_format和t_format
中添加更多格式,它仍然有效PublisherName Description numbers
------------- ----------- -------
examplename1 pdf 2
examplename2 book 1
examplename2 pdf 3
如果你想要列出你的问题,你可以做一个支点 但是当有新格式时,这种新格式不会出现在结果中,因此每次添加新格式时都必须修改查询
select * from ( select p.Name,
f.Description,
count(f.Id) as numbers
from @t_book b
inner join @t_publisher p on b.idPublisher = p.Id
inner join @t_book_format bf on b.Id = bf.idBook
inner join @t_format f on bf.idFormat = f.Id
group by p.Name, f.Description
) t
pivot
( sum(numbers)
for Description in (pdf, book)
) piv
结果现在看起来像这样
Name pdf book
---- --- ----
examplename1 2 null
examplename2 3 1
我想你可以把它放在一个存储过程中并在动态sql中构建最后一个查询,所以当你添加新格式时它仍会继续工作
答案 2 :(得分:0)
试试这个:
select P.Name as PublisherName,
SUM(case when F.Code = 'PDF' then 1 else 0 end) as Number_PDF,
SUM(case when F.Code = 'BOOK' then 1 else 0 end) as Number_BOOK
from T_BOOK as B
join T_BOOK_FORMAT as BF on B.Id = BF.BookId
join T_FORMAT as F on BF.IdFormat = F.Id
join T_PUBLISHER as P on B.IdPublisher = P.Id
group by P.Name
我认为,这本书的格式保存在T_FORMAT
列Code
中(F.Code
部分)。
答案 3 :(得分:0)
您可以使用此代码
SELECT publisher.Name as PublisherName,
(SELECT COUNT(*)
FROM T_BOOK book
JOIN T_BOOK_FORMAT bookformat ON book.Id = bookformat.IdBook
JOIN T_FORMAT format ON bookformat.IdFormat = format.Id
WHERE book.IdPublisher = publisher.Id and format.Code='PDF'
) AS Number_PDF,
(SELECT COUNT(*)
FROM T_BOOK book
JOIN T_BOOK_FORMAT bookformat ON book.Id = bookformat.IdBook
JOIN T_FORMAT format ON bookformat.IdFormat = format.Id
WHERE book.IdPublisher = publisher.Id and format.Code='EBOOK'
) AS Number_BOOK
FROM T_PUBLISHER publisher
GROUP BY publisher.Id
答案 4 :(得分:0)
你走了:
CREATE TABLE Author (
AuthorID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
AuthorName VARCHAR (25) NOT NULL
);
CREATE TABLE Books (
BookID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
BookName VARCHAR (25) NOT NULL,
BookAothor INT NOT NULL,
CONSTRAINT PK_AuthorOfBook FOREIGN KEY (BookAothor) REFERENCES Author (AuthorID)
);
CREATE TABLE Formats (
FormatID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
FormatName VARCHAR (10) NOT NULL
);
CREATE TABLE BookFormat (
BookID INT NOT NULL,
FormatID INT NOT NULL,
CONSTRAINT PK_BookFormats PRIMARY KEY (BookID, FormatID),
CONSTRAINT FK_BookRef FOREIGN KEY (BookID) REFERENCES Books (BookID),
CONSTRAINT FK_FormatRef FOREIGN KEY (FormatID) REFERENCES Formats (FormatID)
);
INSERT INTO Author VALUES
('Author1'), ('Author2'), ('Author3');
INSERT INTO Books VALUES
('Book1', 1), ('Book2', 1),
('Book3', 2), ('Book4', 2),
('Book5', 2), ('Book6', 2);
INSERT INTO Formats VALUES
('PDF'), ('E-Book');
INSERT INTO BookFormat VALUES
(1, 1), (2,1),
(3,1), (4,1), (5,1), (6,2);
SELECT A.AuthorName,
ISNULL(Pdf.PdfBooks, 0) AS PdfBooks,
ISNULL(EBook.EBooks, 0) AS EBooks
FROM
Author A LEFT JOIN
(
SELECT A.AuthorID, COUNT(B.BookID) AS PdfBooks
FROM Author A JOIN Books B ON A.AuthorID = B.BookAothor
INNER JOIN BookFormat BF ON B.BookID = BF.BookID
WHERE BF.FormatID =1
GROUP BY A.AuthorID
) Pdf ON A.AuthorID = Pdf.AuthorID
FULL JOIN
(
SELECT A.AuthorID, COUNT(B.BookID) AS EBooks
FROM Author A JOIN Books B ON A.AuthorID = B.BookAothor
INNER JOIN BookFormat BF ON B.BookID = BF.BookID
WHERE BF.FormatID =2
GROUP BY A.AuthorID
) EBook
ON Pdf.AuthorID = EBook.AuthorID;
结果:
+------------+----------+--------+
| AuthorName | PdfBooks | EBooks |
+------------+----------+--------+
| Author1 | 2 | 0 |
| Author2 | 3 | 1 |
| Author3 | 0 | 0 |
+------------+----------+--------+
这是 demo