2011-02-16 52 views
0

我有查詢,如:SQL UPDATE:你不能在FROM子句指定更新目標表 'nilai_mhs'「

UPDATE nilai_mhs 
SET status_nilai = 3 
where 
id_kode_mk = 
(SELECT e.id_kode_mk 
FROM nilai_mhs c, tbl_mhs d,master_mk e,tbl_mk_pertahun f 
WHERE 
c.nim = d.nim 
AND 
c.id_kode_mk = e.id_kode_mk 
AND 
e.id_kode_mk = f.id_kode_mk 
AND 
c.nim = '20081001010125' 
AND 
c.status_nilai =2 
AND 
sem_ganjil_genap = 'ganjil') 

但結果卻顯示錯誤:

You can't specify target table 'nilai_mhs' for update in FROM clause

人幫助我?

回答

1

就直接更新表沒有一個子查詢

UPDATE nilai_mhs c 
    JOIN tbl_mhs d USING(nim) 
    JOIN master_mk e USING(id_kode_mk) 
    JOIN tbl_mk_pertahun f USING(id_kode_mk) 
SET c.status_nilai = 3 
WHERE c.nim = '20081001010125' 
    AND c.status_nilai = 2 
    AND sem_ganjil_genap = 'ganjil' 

如果您需要執行的子查詢(比如你是groupping結果),你需要加入子查詢來代替:

UPDATE nilai_mhs t JOIN (
    SELECT e.id_kode_mk 
    FROM nilai_mhs c, tbl_mhs d,master_mk e,tbl_mk_pertahun f 
    WHERE c.nim = d.nim AND c.id_kode_mk = e.id_kode_mk AND e.id_kode_mk = f.id_kode_mk AND c.nim = '20081001010125' AND c.status_nilai =2 AND sem_ganjil_genap = 'ganjil' 
) q ON t.id_kode_mk = q.id_kode_mk 
SET t.status_nilai = 3 
+0

謝謝... 這是工作... – 2011-02-16 08:31:35

相關問題