MySQL Workbench - 使用 WHERE 子句的 UPDATE 语句

发布于 2025-01-13 07:53:32 字数 408 浏览 3 评论 0原文

我正在尝试使用另一个名为“new_no2”的表-列名称“NO2”中的数据来更新一个名为“Rural”的表-列名称“NO2”。请参阅下面的代码

update Rural
set Rural.NO2 = new_no2.NO2
where Rural.Year = new_no2.Year
and Rural.Site = new_no2.Site;

但是,我一直收到错误代码:1054。“Where 子句”中的未知列“new_no2.Year”

我浏览了很多教程并尝试了很多操作,例如确保表名正确等等,但由于某种原因,它不起作用。

如果有人能澄清我可能做错了什么,那就太好了。

顺便说一句,我正在使用适用于 MacBook 的 MySQL Workbench 版本 8

谢谢 布鲁诺

I'm trying to update a table called 'Rural' - column name 'NO2', with data from another table called 'new_no2' - column name 'NO2'. see the code below

update Rural
set Rural.NO2 = new_no2.NO2
where Rural.Year = new_no2.Year
and Rural.Site = new_no2.Site;

However, I keep having the error code: 1054. Unknown column 'new_no2.Year' in 'Where clause'

I went through a lot of tutorials and tried loads of things like make sure the table name is correct and so on, but for some reason, it does not work.

It will be great if someone can give some clarity on what I might be doing wrong.

by the way, I'm using MySQL workbench version 8 for MacBook

Thanks
Bruno

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

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

发布评论

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

评论(1

优雅的叶子 2025-01-20 07:53:32

使用子查询:

update Rural set
Rural.NO2 = coalesce((
    select new_no2.NO2
    from new_no2
    where Rural.Year = new_no2.Year
    and Rural.Site = new_no2.Site), no2)

请参阅现场演示

如果在 new_no2 中找不到对应的值,请使用 coalesce() 保持 no2 不变。

Use a sub query:

update Rural set
Rural.NO2 = coalesce((
    select new_no2.NO2
    from new_no2
    where Rural.Year = new_no2.Year
    and Rural.Site = new_no2.Site), no2)

See live demo.

Use coalesce() to leave no2 unchanged if no corresponding value is found in new_no2.

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