如何修复右外连接
How to Fix Right Outer Join
我正在尝试让左外连接使用下面的代码;
使用(LEFT OUTER、RIGHT OUTER、FULL OUTER 和 INNER JOIN)的结果是相同的结果。
我正在尝试 return 所有出版商名称包括那些尚未与任何书籍联系的人!
SELECT P.PublisherName,
COUNT(B.BookID) AS BookPublished
FROM LR_Publisher AS P LEFT OUTER JOIN LR_Book As B
ON P.PublisherID = B.PublisherID
WHERE (P.PublisherID = @pPublisherID OR @pPublisherID IS NULL)
GROUP BY PublisherName
提前致谢
简单的左连接将帮助您获得所有出版商名称,包括尚未出版任何书籍的人
SELECT P.PublisherName
FROM LR_Publisher AS P LEFT OUTER JOIN LR_Book As B
ON P.PublisherID = B.PublisherID
没有出版书籍的出版商被GROUP BY
过滤掉。
可以使用 GROUP BY ALL
:
修复
SELECT P.PublisherName,
COUNT(B.BookID) AS BookPublished
FROM LR_Publisher AS P
LEFT OUTER JOIN LR_Book As B ON P.PublisherID = B.PublisherID
-- WHERE (P.PublisherID = @pPublisherID OR @pPublisherID IS NULL)
GROUP BY PublisherName
你可以这样写
select P.PublisherName,
isnull(bi.BookPublished, 0) as BookPublished
from LR_Publisher as P
left join (
select B.PublisherID, Count(B.BookID) BookPublished
from LR_Book as B
where (@pPublisherID is null or B.PublisherID = @pPublisherID)
group by B.PublisherID
) bi on P.PublisherID = Bi.PublisherID
where (@pPublisherID is null or P.PublisherID = @pPublisherID)
我也不知道分组依据进行的过滤。谢谢提问。
我正在尝试让左外连接使用下面的代码; 使用(LEFT OUTER、RIGHT OUTER、FULL OUTER 和 INNER JOIN)的结果是相同的结果。 我正在尝试 return 所有出版商名称包括那些尚未与任何书籍联系的人!
SELECT P.PublisherName,
COUNT(B.BookID) AS BookPublished
FROM LR_Publisher AS P LEFT OUTER JOIN LR_Book As B
ON P.PublisherID = B.PublisherID
WHERE (P.PublisherID = @pPublisherID OR @pPublisherID IS NULL)
GROUP BY PublisherName
提前致谢
简单的左连接将帮助您获得所有出版商名称,包括尚未出版任何书籍的人
SELECT P.PublisherName
FROM LR_Publisher AS P LEFT OUTER JOIN LR_Book As B
ON P.PublisherID = B.PublisherID
没有出版书籍的出版商被GROUP BY
过滤掉。
可以使用 GROUP BY ALL
:
SELECT P.PublisherName,
COUNT(B.BookID) AS BookPublished
FROM LR_Publisher AS P
LEFT OUTER JOIN LR_Book As B ON P.PublisherID = B.PublisherID
-- WHERE (P.PublisherID = @pPublisherID OR @pPublisherID IS NULL)
GROUP BY PublisherName
你可以这样写
select P.PublisherName,
isnull(bi.BookPublished, 0) as BookPublished
from LR_Publisher as P
left join (
select B.PublisherID, Count(B.BookID) BookPublished
from LR_Book as B
where (@pPublisherID is null or B.PublisherID = @pPublisherID)
group by B.PublisherID
) bi on P.PublisherID = Bi.PublisherID
where (@pPublisherID is null or P.PublisherID = @pPublisherID)
我也不知道分组依据进行的过滤。谢谢提问。