2014-04-25 61 views
0

我一直在sql server 2008中使用多個表,並且數據用於傳統ASP應用程序。 以下查詢(請參閱下面的內容)已用於使用複選框填充「更新」頁面。第一個表格(KSA)填充複選框,而第二個表格(KSA_Out)提供用戶選中的複選框。對於每個Objective都有一組KSA複選框,我遇到的問題是我希望KSA左連接仍然填充表單的複選框,即使連接表中沒有匹配。左外連接,多表,填充經典ASP應用程序中的複選框

應用程序背後的想法是這樣的: 有一個包含表格的頁面:每個目標有一行,每個結果有一列。 該表由Objectives表和Outcomes表填充,每個單元格中有一個「Add/Edit」鏈接,以及一個具有Querystring的URL,將ObjectiveID和OutcomeID傳遞到下一頁,該頁更新Objective/Outcome組合。

在更新頁面上,有一個用複選框填充表單的查詢。 KSA表格填充列表項目,而OutcomesKSA表格以檢查複選框(OutcomesKSA.KSA_Value字段,布爾值)的形式提供任何先前用戶輸入的數據。 ObjOut表是連接目標和結果表的聯結表。

在應用程序中,如果用戶輸入的複選框數據存在(OutcomesKSA.KSA_Value字段,它是布爾值),但我仍然只獲取某個目標/結果組合的數據如果沒有數據輸入到OutcomesKSA表中,則從KSA表中列出。

目前,沒有填充任何內容,使用我正在使用的查詢(請參閱下文)。

感謝您的幫助。

這裏是如何的數據將顯示,從桌到桌,以顯示數據是如何分散和相關的(我只包含的PK和FKS,表明它們之間的關係):

Objectives table 
ID 
5 

Outcomes table 
ID 
4 


ObjOut table 
ID|ObjectiveID|OutcomeID 
1|5|4 

OutcomesKSA table 
ID|ObjOutID|KSA_ID 
2|1|1 
3|1|2 
4|1|3 


KSA table 
ID 
1 
2 
3 

查詢:

SELECT 
KSA.ID as KSA_ID, KSA.KSA_Version, KSA.KSA_Sort_Order_Number, 
KSA.KSA_Outcome_Number, KSA.KSA_Category as KSA_KSA_Category, 
KSA.KSA_Category_Sub_Num, KSA.KSA_Category_Sub_Num_Descr, 
KSA.KSA_Category_Sub_Num_Sub_Alpha, KSA.KSA_Category_Sub_Num_Sub_Alpha_Descr, 
KSA.KSA_ID as KSA_KSA_ID, KSA.KSA_Descr_Combined as KSA_KSA_Descr_Combined, 
KSA.LastUpdate, KSA.Date_Created, 

OutcomesKSA.KSA_ID as OutcomesKSA_KSA_ID,OutcomesKSA.KSA_Value as OutcomesKSA_KSA_Value,OutcomesKSA.ObjOutID, 

ObjOut.ObjectiveID, ObjOut.OutcomeID, 

Objectives.ID as Obj_Obj_ID 


FROM 
KSA 
LEFT OUTER JOIN 
OutcomesKSA 
ON 
KSA.ID=OutcomesKSA.KSA_ID 
INNER JOIN 
ObjOut 
ON 
OutcomesKSA.ObjOutID=ObjOut.ID 
INNER JOIN 
Objectives 
ON 
ObjOut.ObjectiveID=Objectives.ID 

WHERE 
KSA.KSA_Outcome_Number ='1' 
AND 
KSA.KSA_Category ='k' 
AND 
Objectives.ID ='29' 

ORDER BY KSA.KSA_Sort_Order_Number ASC; 

的表格:

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

SET ANSI_PADDING ON 
GO 

CREATE TABLE [dbo].[Objectives](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [CourseID] [int] NOT NULL, 
    [Objective] [varchar](max) NULL, 
    [LastUpdate] [datetime] NULL, 
    [Date_Created] [datetime] NULL, 
CONSTRAINT [PK_Objectives] PRIMARY KEY CLUSTERED 
(
    [ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

SET ANSI_PADDING OFF 
GO 

ALTER TABLE [dbo].[Objectives] WITH NOCHECK ADD CONSTRAINT [FK_Objectives_Courses] FOREIGN KEY([CourseID]) 
REFERENCES [dbo].[Courses] ([CourseID]) 
ON UPDATE CASCADE 
GO 

ALTER TABLE [dbo].[Objectives] CHECK CONSTRAINT [FK_Objectives_Courses] 
GO 

ALTER TABLE [dbo].[Objectives] ADD CONSTRAINT [DF_Objectives_Date_Created] DEFAULT (getdate()) FOR [Date_Created] 
GO 

============= 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

SET ANSI_PADDING ON 
GO 

CREATE TABLE [dbo].[Outcomes](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [OutcomeGroup] [varchar](50) NULL, 
    [OutcomeType] [varchar](50) NULL, 
    [OutcomeNumber] [int] NULL, 
    [OutcomeName] [varchar](500) NULL, 
    [OutcomeDescription] [varchar](max) NULL, 
    [OutcomeVersionYear] [varchar](50) NULL, 
    [OutcomeVersionSemester] [varchar](50) NULL, 
    [LastUpdate] [datetime] NULL, 
    [Date_Created] [datetime] NULL, 
CONSTRAINT [PK_Outcomes] PRIMARY KEY CLUSTERED 
(
    [ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

SET ANSI_PADDING OFF 
GO 

ALTER TABLE [dbo].[Outcomes] ADD CONSTRAINT [DF_Outcomes_Date_Created] DEFAULT (getdate()) FOR [Date_Created] 
GO 


========== 


SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[ObjOut](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [ObjectiveID] [int] NULL, 
    [OutcomeID] [int] NULL, 
    [LastUpdate] [datetime] NULL, 
    [Date_Created] [datetime] NULL, 
CONSTRAINT [PK_ObjOut] PRIMARY KEY CLUSTERED 
(
    [ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

ALTER TABLE [dbo].[ObjOut] WITH CHECK ADD CONSTRAINT [FK_ObjOut_Objectives] FOREIGN KEY([ObjectiveID]) 
REFERENCES [dbo].[Objectives] ([ID]) 
ON UPDATE CASCADE 
ON DELETE CASCADE 
GO 

ALTER TABLE [dbo].[ObjOut] CHECK CONSTRAINT [FK_ObjOut_Objectives] 
GO 

ALTER TABLE [dbo].[ObjOut] WITH CHECK ADD CONSTRAINT [FK_ObjOut_Outcomes] FOREIGN KEY([OutcomeID]) 
REFERENCES [dbo].[Outcomes] ([ID]) 
ON UPDATE CASCADE 
ON DELETE CASCADE 
GO 

ALTER TABLE [dbo].[ObjOut] CHECK CONSTRAINT [FK_ObjOut_Outcomes] 
GO 

ALTER TABLE [dbo].[ObjOut] ADD CONSTRAINT [DF_ObjOut_Date_Created] DEFAULT (getdate()) FOR [Date_Created] 
GO 







=========== 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[OutcomesKSA](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [ObjOutID] [int] NOT NULL, 
    [KSA_ID] [int] NULL, 
    [KSA_Value] [bit] NULL, 
    [LastUpdate] [datetime] NULL, 
    [Date_Created] [datetime] NULL, 
CONSTRAINT [PK_OutcomesKSA] PRIMARY KEY CLUSTERED 
(
    [ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

ALTER TABLE [dbo].[OutcomesKSA] WITH CHECK ADD CONSTRAINT [FK_OutcomesKSA_ObjOut] FOREIGN KEY([ObjOutID]) 
REFERENCES [dbo].[ObjOut] ([ID]) 
ON UPDATE CASCADE 
ON DELETE CASCADE 
GO 

ALTER TABLE [dbo].[OutcomesKSA] CHECK CONSTRAINT [FK_OutcomesKSA_ObjOut] 
GO 

ALTER TABLE [dbo].[OutcomesKSA] ADD CONSTRAINT [DF_OutcomesKSA_Date_Created] DEFAULT (getdate()) FOR [Date_Created] 



========================= 







SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

SET ANSI_PADDING ON 
GO 

CREATE TABLE [dbo].[KSA](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [KSA_Version] [varchar](50) NULL, 
    [KSA_Sort_Order_Number] [int] NULL, 
    [KSA_Outcome_Number] [varchar](50) NULL, 
    [KSA_Outcome_Number_Sort] [varchar](50) NULL, 
    [KSA_Category] [char](1) NULL, 
    [KSA_Category_Sub_Num] [varchar](50) NULL, 
    [KSA_Category_Sub_Num_Sort] [varchar](50) NULL, 
    [KSA_Category_Sub_Num_Descr] [varchar](max) NULL, 
    [KSA_Category_Sub_Num_Sub_Alpha] [char](1) NULL, 
    [KSA_Category_Sub_Num_Sub_Alpha_Descr] [varchar](1000) NULL, 
    [KSA_ID] [char](8) NULL, 
    [KSA_ID_Sort] [char](8) NULL, 
    [KSA_Descr_Combined] [varchar](max) NULL, 
    [LastUpdate] [datetime] NULL, 
    [Date_Created] [datetime] NULL, 
CONSTRAINT [PK_KSA] PRIMARY KEY CLUSTERED 
(
    [ID] 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 

SET ANSI_PADDING OFF 
GO 

ALTER TABLE [dbo].[KSA] ADD CONSTRAINT [DF_KSA_Date_Created] DEFAULT (getdate()) FOR [Date_Created] 
GO 
+2

這個SQL讓我很頭疼。但無論如何,你從SELECT語句得到的結果是什麼? – Siyual

+0

我知道你的意思。從SELECT語句返回0行。 – buck1112

回答

1

後來INNER JOINs是EFFE將OUTER JOIN重新轉換爲INNER。您可以構建查詢,以便將多個表格INNER JOINed放在一起,然後將該集合應用爲OUTER JOIN

此外,WHERE子句後所有的聯接施加並且將過濾整個行,所以與OUTER JOIN有將濾膜放在所述WHERE子句和ON子句中之間的差。

關於編碼風格的一個注意事項。雖然在代碼格式化方面有很多個人偏好,但通常應該有易於閱讀和理解的目標。我鼓勵你考慮一種不同的格式,無論是在向別人展示代碼的時候,還是在你試圖瞭解你在6個月內寫的內容時。

把它放在一起,這應該是更多你想要的。

SELECT 
    KSA.ID as KSA_ID 
    ,KSA.KSA_Version 
    ,KSA.KSA_Sort_Order_Number 
    ,KSA.KSA_Outcome_Number 
    ,KSA.KSA_Category as KSA_KSA_Category 
    ,KSA.KSA_Category_Sub_Num 
    ,KSA.KSA_Category_Sub_Num_Descr 
    ,KSA.KSA_Category_Sub_Num_Sub_Alpha 
    ,KSA.KSA_Category_Sub_Num_Sub_Alpha_Descr 
    ,KSA.KSA_ID as KSA_KSA_ID 
    ,KSA.KSA_Descr_Combined as KSA_KSA_Descr_Combined 
    ,KSA.LastUpdate 
    ,KSA.Date_Created 
    ,OutcomesKSA.KSA_ID as OutcomesKSA_KSA_ID 
    ,OutcomesKSA.KSA_Value as OutcomesKSA_KSA_Value 
    ,OutcomesKSA.ObjOutID 
    ,ObjOut.ObjectiveID 
    ,ObjOut.OutcomeID 
    ,Objectives.ID as Obj_Obj_ID 
FROM 
    KSA 
    LEFT OUTER JOIN (
     OutcomesKSA 
     INNER JOIN ObjOut 
      ON OutcomesKSA.ObjOutID = ObjOut.ID 
     INNER JOIN Objectives 
      ON ObjOut.ObjectiveID = Objectives.ID 
    ) ON KSA.ID = OutcomesKSA.KSA_ID 
     AND Objectives.ID = 29 --filter 
WHERE 
    KSA.KSA_Outcome_Number = '1' 
    AND KSA.KSA_Category = 'k' 
ORDER BY KSA.KSA_Sort_Order_Number; 
+0

這非常有幫助。我很感謝你的全面回答和幫助。 – buck1112

+0

是否有可能獲得兄弟表的左外連接? 在與KSA表相同的級別和相同的關係中添加了一個新表。 KSA表和名爲KSA_Priority的新表都具有KSA_Out聯結表中的外鍵。在相同的查詢中,是否可以從KSA和KSA_Priority表中獲得所有結果?謝謝。 – buck1112