我一直在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
這個SQL讓我很頭疼。但無論如何,你從SELECT語句得到的結果是什麼? – Siyual
我知道你的意思。從SELECT語句返回0行。 – buck1112