SQL 更新:您无法指定目标表“nilai_mhs”;用于 FROM 子句中的更新“

发布于 2024-10-17 18:30:36 字数 458 浏览 3 评论 0原文

我有这样的查询:

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')

但结果显示错误:

您无法在 FROM 子句中指定要更新的目标表“nilai_mhs”

有人可以帮助我吗?

I have query like:

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')

but the result displays error:

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

Can anybody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-10-24 18:30:36

只需直接更新表而不使用子查询

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'

如果您需要执行子查询(例如对结果进行分组),则需要加入子查询:

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

Just update the table directly without a subquery

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'

If you need to perform a subquery (for example you are groupping results) you need to join the subquery instead:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文