SQL一对多联接每个联接获得一个结果
我想将数据从SQL 2008服务器导出到文件,csv或excel不重要。在数据库中,我有两个实体,问题和答案。问题是这些问题有多个答案。然后,联接将导致每个问题有多行。我该怎么做?结果是每个问题和答案只有一行,每个答案只需添加一个新列。像这样: 表结构:SQL一对多联接每个联接获得一个结果,sql,Sql,我想将数据从SQL 2008服务器导出到文件,csv或excel不重要。在数据库中,我有两个实体,问题和答案。问题是这些问题有多个答案。然后,联接将导致每个问题有多行。我该怎么做?结果是每个问题和答案只有一行,每个答案只需添加一个新列。像这样: 表结构: Question ------------- Id Text Category Answer ------------- Id Text IsCorrect QuestionId 结果示例: Col1 Col2
Question
-------------
Id
Text
Category
Answer
-------------
Id
Text
IsCorrect
QuestionId
结果示例:
Col1 Col2 Col3 Col4 Col5 Col6 Col7
Result1: Question1, Question1Text, Answer1Prop1, Answer1Prop2, Answer2Prop1, Answer2Prop2, null
Result2: Question2, Question2Text, Answer3Prop1, Answer3Prop2, Answer4Prop1, Answer4Prop2, Answer5Prop3
如果您可以使用包含所有问题和多行答案的联接生成一个结果,如您所述,我认为您可以使用光标在所有问题和答案上循环,然后使用一个诱人的/“hashtable”(#tablofAnwers)来保存最终结果。对于一个问题的每个答案,您都会更改#表并添加一个新列 像这样的事情可能会让你走上正确的方向 如果您的结果如下所示: (在我的示例中,我将以下结果存储在名为“#q”的临时表中) 然后可以在结果上声明一个游标
Declare @questionId int
, @questionText varchar(max)
, @prevId int
, @colNo int = 1
, @colMax int = 0
, @i int = 1
, @sql nvarchar(max)
, @nulls nvarchar(max) = ''
Create table #tempTable
(
QuestionId int not null,
Col1 varchar(max) null
)
Declare question_cursor cursor for
Select QuestionId, text
From #q
Open question_cursor
FETCH NEXT FROM question_cursor
INTO @questionId, @questionText
Set @prevId = 0
WHILE @@FETCH_STATUS = 0
BEGIN
If (@prevId = @questionId)
Begin
If (@colNo > @colMax)
Begin
Set @sql = N'alter table #tempTable add Col' + cast(@colNo as varchar) + ' varchar(max) null'
exec sp_executesql @statement = @sql
Set @colMax = @colMax + 1
End
Set @sql = N'update #tempTable set Col' + cast(@colNo as varchar) + ' = ''' + @questionText + ''' where QuestionId = ' + cast(@questionId as varchar)
print @sql
exec sp_executesql @statement = @sql
Set @colNo = @colNo + 1
End
Else Begin
Set @prevId = @questionId
Set @colNo = 1
while (@i <= @colMax)
begin
set @nulls = @nulls + ', null'
set @i = @i + 1
end
Set @sql = N'insert into #tempTable values (' + cast(@questionId as varchar) + ', ''' + @questionText + '''' + isnull(@nulls, '') +')'
print @sql
exec sp_executesql @statement = @sql
Set @colNo = @colNo + 1
End
FETCH NEXT FROM question_cursor
INTO @questionId, @questionText
END
Close question_cursor
Deallocate question_cursor
Select * from #tempTable
不像.Net(甚至javascript)处理这样的问题那样优雅。。但是,你打算怎么办?:)
我希望这能对你的问题有所帮助:)
要将其导出到文件,我只需在Sql Management Studio中复制/粘贴结果,然后将其粘贴到Excel或其他文件中;) 请让我朝着正确的方向走,而不是投反对票……在你的问题中加入更详细的内容,特别是表格结构。我将研究您可以使用的联接类型:
Declare @questionId int
, @questionText varchar(max)
, @prevId int
, @colNo int = 1
, @colMax int = 0
, @i int = 1
, @sql nvarchar(max)
, @nulls nvarchar(max) = ''
Create table #tempTable
(
QuestionId int not null,
Col1 varchar(max) null
)
Declare question_cursor cursor for
Select QuestionId, text
From #q
Open question_cursor
FETCH NEXT FROM question_cursor
INTO @questionId, @questionText
Set @prevId = 0
WHILE @@FETCH_STATUS = 0
BEGIN
If (@prevId = @questionId)
Begin
If (@colNo > @colMax)
Begin
Set @sql = N'alter table #tempTable add Col' + cast(@colNo as varchar) + ' varchar(max) null'
exec sp_executesql @statement = @sql
Set @colMax = @colMax + 1
End
Set @sql = N'update #tempTable set Col' + cast(@colNo as varchar) + ' = ''' + @questionText + ''' where QuestionId = ' + cast(@questionId as varchar)
print @sql
exec sp_executesql @statement = @sql
Set @colNo = @colNo + 1
End
Else Begin
Set @prevId = @questionId
Set @colNo = 1
while (@i <= @colMax)
begin
set @nulls = @nulls + ', null'
set @i = @i + 1
end
Set @sql = N'insert into #tempTable values (' + cast(@questionId as varchar) + ', ''' + @questionText + '''' + isnull(@nulls, '') +')'
print @sql
exec sp_executesql @statement = @sql
Set @colNo = @colNo + 1
End
FETCH NEXT FROM question_cursor
INTO @questionId, @questionText
END
Close question_cursor
Deallocate question_cursor
Select * from #tempTable
QuestionId Col1 Col2 Col3 Col4
1 text1 text2 text3 text4
2 text1 text2 NULL NULL