2016-11-14 73 views
0

下面的查詢似乎不工作,並給語法錯誤:Informix的更新加入

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

Informix語法手冊不包含第一種形式;沒有理由認爲它會起作用。 –

回答

0

通常需要一個語句級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子句的情況下,表中的所有行(const_acad_record)都會更新,而那些沒有與SELECT匹配的條目的行將被設置爲NULL

如果我理解表更好,我可能會使用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是來自表school_category_type還是來自表sch_rec;這可能會改變什麼是合適的。

+0

感謝您的快速響應,喬納森。來自school_category_type的education_level正在更新。如果是這種情況,哪個是最好的查詢? 關於Manohar –

+0

檢查查詢計劃,但我希望IN變種更好。通常... –