以下查询似乎无效并且语法错误:
update const_acad_record
set const_acad_record.education_level = school_category_type.education_level
FROM school_category_type, sch_rec sch_rec
where const_acad_record.organization_ID = sch_rec.id and
sch_rec.ctgry = school_category_type.code
如果我将此转换为子查询,如下所示,它可以工作,但会更新比所需更多的记录:
update const_acad_record
set education_level = (SELECT education_level
FROM school_category_type sct, sch_rec sr
where const_acad_record.organization_ID = sr.id and
sr.ctgry = sct.code)
(education_level
列来自表格school_category_type
。)
答案 0 :(得分:0)
您通常还需要一个语句级IsKeyboardFocusWithin
子句,以将更新的记录数限制为具有匹配条目的记录。我认为在这个例子中,它可以采取以下形式:
WHERE
未经测试的SQL
这会将行更新限制为具有匹配记录的行。如果没有语句级WHERE子句,则表(UPDATE const_acad_record
SET education_level = (SELECT education_level
FROM school_category_type sct
JOIN sch_rec sr
ON sr.ctgry = sct.code
WHERE const_acad_record.organization_ID = sr.id)
WHERE EXISTS(SELECT education_level
FROM school_category_type sct
JOIN sch_rec sr
ON sr.ctgry = sct.code
WHERE const_acad_record.organization_ID = sr.id)
)中的所有行都会更新,而const_acad_record
中没有匹配条目的行将设置为SELECT
。
如果我更好地理解表格,我可能会在语句级查询中使用IN子句,例如:
NULL
未经测试的SQL
在其他问题中,不清楚列UPDATE const_acad_record
SET Education_Level = (SELECT education_level
FROM school_category_type sct
JOIN sch_rec sr
ON sr.ctgry = sct.code
WHERE const_acad_record.organization_ID = sr.id)
WHERE Organization_ID IN (SELECT sr.id
FROM school_category_type sct
JOIN sch_rec sr
ON sr.ctgry = sct.code)
是来自表education_level
还是表school_category_type
;这可能会改变适当的。