SQL Server 问题
我在 sql server 2008 上遇到问题,我有 2 个表 SlideShow 和 Slide。两个表中的公共字段是 SlideShowId。 现在要使用 SlideCount 获取所有幻灯片,我使用此过程。
ALTER PROCEDURE [dbo].[sp_GetAllSlideShow]
@FILTER BIT,
@PORTALID INT
AS
BEGIN
IF (@FILTER = 1)
SELECT SS.SLIDESHOWNAME + ' [' + CAST((COUNT(S.SLIDEID)) AS VARCHAR(MAX))+']', SS.SLIDESHOWID FROM SLIDESHOW SS
INNER JOIN SLIDES S
ON SS.SLIDESHOWID = S.SLIDESHOWID
WHERE SS.PORTALID = 0 AND [TYPE] IS NULL
GROUP BY SS.SLIDESHOWNAME, SS.SLIDESHOWID
ELSE
SELECT SS.SLIDESHOWNAME + ' [' + CAST((COUNT(S.SLIDEID)) AS VARCHAR(MAX))+']', SS.SLIDESHOWID FROM SLIDESHOW SS
INNER JOIN SLIDES S
ON SS.SLIDESHOWID = S.SLIDESHOWID
WHERE SS.PORTALID = @PORTALID
GROUP BY SS.SLIDESHOWNAME, SS.SLIDESHOWID
END
这运行正常,但是如果已经创建了新的幻灯片,则在该幻灯片中创建新幻灯片之前,此过程不会显示该幻灯片,实际上这是因为我已经加入了 SlideCount 的两个表,并且没有幻灯片特别的幻灯片,该幻灯片不会显示,但我也想要该幻灯片。默认情况下,其中的计数应为 0。
我有什么办法可以做到这一点吗?
i have a issue on sql server 2008, i have 2 tables SlideShow and Slide. common field in both the table is SlideShowId.
now to get all the SlideShow with SlideCount i use this procedure.
ALTER PROCEDURE [dbo].[sp_GetAllSlideShow]
@FILTER BIT,
@PORTALID INT
AS
BEGIN
IF (@FILTER = 1)
SELECT SS.SLIDESHOWNAME + ' [' + CAST((COUNT(S.SLIDEID)) AS VARCHAR(MAX))+']', SS.SLIDESHOWID FROM SLIDESHOW SS
INNER JOIN SLIDES S
ON SS.SLIDESHOWID = S.SLIDESHOWID
WHERE SS.PORTALID = 0 AND [TYPE] IS NULL
GROUP BY SS.SLIDESHOWNAME, SS.SLIDESHOWID
ELSE
SELECT SS.SLIDESHOWNAME + ' [' + CAST((COUNT(S.SLIDEID)) AS VARCHAR(MAX))+']', SS.SLIDESHOWID FROM SLIDESHOW SS
INNER JOIN SLIDES S
ON SS.SLIDESHOWID = S.SLIDESHOWID
WHERE SS.PORTALID = @PORTALID
GROUP BY SS.SLIDESHOWNAME, SS.SLIDESHOWID
END
this runs fine, but say if new slideshow has been created, this procedure does not show up that slideshow until new slide has been created in that slideshow, actually that is because i have joined both the tables for SlideCount, and there is no slide for particular slideshow, that slideshow wont show up, but i also want that slideshow. the count in that should be 0 by default.
is there any way i can do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您使用的是
INNER JOIN
。INNER JOIN
只会显示两个表中相同的结果,因此,如果您的幻灯片记录仅添加到一个表中,则INNER JOIN
不会将其拉入 <代码>选择。尝试将INNER JOIN
更改为LEFT JOIN
。That is because you are using an
INNER JOIN
. AnINNER JOIN
will only show results that are equal in both tables, so if your slideshow record was added to only one table then theINNER JOIN
would not pull it in theSELECT
. Try changing yourINNER JOIN
to aLEFT JOIN
.