在SQL查询中,如何仅在特定条件下进行连接?

时间:2012-02-22 14:14:57

标签: sql sql-server-2008

我有以下SQL查询:

select Subjects.S_ID as ID, 
Subjects.S_ParentID as ParentID, 
Subjects.S_Name as Name,
Subjects.S_Order as [Order],
subjects.Sbj_IsVisible
from Subjects 
left join KPI_SubjectDetails k on Subjects.S_ID = k.S_ID 
where
subjects.Sbj_CourseID = 7594
and subjects.Sbj_Type=2
and subjects.Sbj_IsVisible=1
order by subjects.S_Level,
k.SD_Order

每个主题都有一个s_ParentID。最顶层的主题的s_ParnetID为0。 我想添加一个SQL Join,它将执行以下操作: 如果父Subject设置为Sbj_IsVisible = 0(任何主题可以是父级),则SQL不应输出它或其任何子级。但是,如果s_ParentID设置为0,我不想进行Sbj_IsVisible检查,因为这是最受欢迎的主题。 这就是我得到的:

select Subjects.S_ID as ID, 
Subjects.S_ParentID as ParentID, 
Subjects.S_Name as Name,
Subjects.S_Order as [Order],
subjects.Sbj_IsVisible
from Subjects 
join Subjects_tbl st on Subjects.S_ParentID = st.S_ID and subjects.S_ParentID <> 0
left join KPI_SubjectDetails k on Subjects.S_ID = k.S_ID 
where
subjects.Sbj_CourseID = 7594
and subjects.Sbj_Type=2
and subjects.Sbj_IsVisible=1
and st.Sbj_IsVisible = 1
order by subjects.S_Level,
k.SD_Order

这部分有效。当父主题设置为sbj_Isvisible 0时,它不会返回其子主题。 但是,如果最顶层的主题设置为sbj_IsVisible 1,则最顶层的主题不会输出,但是它的子主题会输出。

BTW,这是一个SQL Server 2008。

//编辑 添加一些示例数据。 这是原始查询的输出:

ID  ParentID    Name    Order   Sbj_IsVisible
9017    0   'Boot Camp' 18  1
9033    9017    1   4   1
9049    9017    test 1  8   1
9050    9049    test 2  1   1

这是我的查询的输出:

ID  ParentID    Name    Order   Sbj_IsVisible
9033    9017    1   4   1
9049    9017    test 1  8   1
9050    9049    test 2  1   1

这是创建表输出:

USE [Fox8]
GO

/****** Object:  Table [dbo].[Subjects_tbl]    Script Date: 02/22/2012 16:25:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Subjects_tbl](
    [S_ID] [int] IDENTITY(1,1) NOT NULL,
    [S_TopID] [int] NULL,
    [S_ParentID] [int] NULL,
    [S_Name] [nvarchar](255) NULL,
    [S_Order] [int] NULL,
    [S_ItemCount] [int] NOT NULL,
    [S_Level] [int] NULL,
    [S_IsInherited] [int] NOT NULL,
    [S_SortType] [nvarchar](50) NULL,
    [S_SortOrder] [nvarchar](50) NULL,
    [OriginalSbj_CourseID] [int] NULL,
    [Sbj_CourseID] [int] NOT NULL,
    [Sbj_IsVisible] [int] NULL,
    [Sbj_SkinType] [int] NULL,
    [CopyOf_SubjectID] [int] NULL,
    [Sbj_GUID] [uniqueidentifier] NULL,
    [Sbj_type] [int] NULL,
    [s_OriginalSubjectID] [int] NULL,
    [OriginalEvalTree_SbjId] [int] NULL,
    [S_IsDeleted] [smallint] NOT NULL,
    [S_DateDeleted] [datetime] NULL,
    [S_IsPrimary] [bit] NULL,
 CONSTRAINT [PK_Subjects] PRIMARY KEY CLUSTERED 
(
    [S_ID] ASC,
    [S_ItemCount] ASC,
    [Sbj_CourseID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [UX_Subjects_S_ID_Sbj_CourseID] UNIQUE NONCLUSTERED 
(
    [S_ID] ASC,
    [Sbj_CourseID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'bitwise field 1 for regular subject 2 for weighted Subject 4 for X of Y Subject' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Subjects_tbl', @level2type=N'COLUMN',@level2name=N'Sbj_type'
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  CONSTRAINT [DF_Subjects_S_ItemCount]  DEFAULT ((0)) FOR [S_ItemCount]
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  CONSTRAINT [DF_Subjects_S_IsInherited]  DEFAULT ((1)) FOR [S_IsInherited]
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  CONSTRAINT [DF_Subjects_Sbj_CourseID]  DEFAULT ((-1)) FOR [Sbj_CourseID]
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  DEFAULT ((0)) FOR [Sbj_SkinType]
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  CONSTRAINT [DF_Subjects_Sbj_IsEvaluation]  DEFAULT ((1)) FOR [Sbj_type]
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  DEFAULT ((0)) FOR [S_IsDeleted]
GO

ALTER TABLE [dbo].[Subjects_tbl] ADD  DEFAULT ((0)) FOR [S_IsPrimary]
GO

1 个答案:

答案 0 :(得分:1)

您的问题对我来说有点混乱,但让我建议使用OR条款,如:

SELECT s.S_ID AS ID, s.S_ParentID AS ParentID, s.S_Name AS Name, 
    s.S_Order AS [Order], s.Sbj_IsVisible
FROM Subjects s
LEFT JOIN Subjects_tbl st ON s.S_ParentID = st.S_ID
LEFT JOIN KPI_SubjectDetails k ON s.S_ID = k.S_ID 
WHERE s.Sbj_CourseID = 7594
    AND s.Sbj_Type=2
    AND s.Sbj_IsVisible = 1
    AND (st.Sbj_IsVisible = 0 OR s.S_ParentID = 0)
ORDER BY s.S_Level, k.SD_Order

基本上,如果相应的父母不可见或者没有相应的父母(以及其他条件意味着什么),请从主题表中选择信息。

希望有所帮助!