informix 从其他 table 更新多连接

informix update from other table with multi joins

以下查询似乎不起作用并给出语法错误:

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 列来自 table school_category_type。)

您通常还需要一个语句级别 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)

未测试SQL

这将行更新限制为有匹配记录的行。在没有语句级 WHERE 子句的情况下,table (const_acad_record) 中的所有行都会更新,而 SELECT 中没有匹配条目的行将设置为 NULL.

如果我能更好地理解 table,我可能会在语句级查询中使用 IN 子句,例如:

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)

未测试SQL

除其他问题外,不清楚列 education_level 是来自 table school_category_type 还是 table sch_rec;这可能会改变合适的内容。