我有下面的SQL语句从多个表中提取数据,可以看出在tblquestions中章节Id为空我想知道如何更新表,以便它显示正在的章节id来自问题表中匹配的部分ID
反过来我想更新tblQuestions,使用chapterId,其中Id部分等于section section的部分
select chap.chapterID , sec.sectionID, q.ChapterId , q.sectionID from tblChapters as chap
left join tblSections as sec on chap.chapterID = sec.chapterID
left join tblQuestions as q on chap.subjectID = q.subjectID
where chap.subjectID = 363
group by chap.chapterID, sec.sectionID, q.ChapterId , q.sectionID
输出
chapterID sectionID ChapterId sectionID
22266 4869 NULL 4869
Tbl章节
chapterID
subjectID
chapterNo
Title
cssID
OldChapterId
tbl sections
sectionID
chapterID
sectionNO
Title
cssID
oldSecId
tblQuestions
questionID
QuestionType
QuestionNo
question
A
B
C
D
correctAnswer
explanation
bookRef
sectionID
subjectID
syllabusRef
noChapSec
ChapterId
oldChapIf
OldSubID
oldSecId
答案 0 :(得分:0)
您可以尝试这样: -
update q
set q.ChapterId=t.chapterID
from tblQuestions q Join (
select chap.chapterID , sec.sectionID, q.ChapterId , q.sectionID from tblChapters as chap
left join tblSections as sec on chap.chapterID = sec.chapterID
left join tblQuestions as q on chap.subjectID = q.subjectID
where chap.subjectID = 363
group by chap.chapterID, sec.sectionID, q.ChapterId , q.sectionID )t on t.sectionID=q.sectionID
答案 1 :(得分:0)
试试这个......
UPDATE tblQuestions q
SET q.ChapterId = (
SELECT sec .ChapterId
from tblSections sec
INNER JOIN tblChapters chap on chap.chapterID = sec.chapterID
WHERE chap.subjectID = q.subjectID
)
WHERE q.ChapterId IS NULL
答案 2 :(得分:0)
UPDATE q SET q.ChapterID = chap.ChapterID
FROM tblQuestions q
INNER JOIN tblChapters chap
ON chap.subjectID = q.subjectID
WHERE q.ChapterId IS NULL