SQL Server 问题

发布于 2024-12-09 11:37:50 字数 989 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-12-16 11:37:50

这是因为您使用的是INNER JOININNER JOIN 只会显示两个表中相同的结果,因此,如果您的幻灯片记录仅添加到一个表中,则 INNER JOIN 不会将其拉入 <代码>选择。尝试将 INNER JOIN 更改为 LEFT JOIN

That is because you are using an INNER JOIN. An INNER JOIN will only show results that are equal in both tables, so if your slideshow record was added to only one table then the INNER JOIN would not pull it in the SELECT. Try changing your INNER JOIN to a LEFT JOIN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文