根据另一列中的相等值更新列值

发布于 2025-01-30 13:48:02 字数 1001 浏览 2 评论 0原文

正在

起点

Parcelid地址
44Street
32Yyyy
44
44
4412
12

XXXX
Street
使用
Workbench
MySQL Zzzz

基本上,如果它们具有相同的包裹,我想填充同一表的同一列。我通过自我加入来读到这一点,我可以解决这个问题,到目前为止,我已经写了这篇文章:有帮助吗?

UPDATE table t1 
INNER JOIN table t2  ON t1.ParcelID = t2.ParcelID
SET t1.Address = IF(t2.Address = '' OR NULL, t2.Address, t1.Address)
WHERE t1.ParcelID = t2.ParcelID;

有什么建议吗?写在其中的条款冗余吗?

预先感谢^^

I'm using MySQL Workbench:

Starting point:

ParcelIDAddress
44street xxxx
32street yyyy
44empty
44null
12empty
12street zzzz

What should be:

ParcelIDAddress
44street xxxx
32street yyyy
44street xxxx
44street xxxx
12street zzzz
12street zzzz

Basically, I want to populate that same column of the same table if they have the same ParcelID. I read around that with a self join I could solve the problem, so far I've written this: any help?

UPDATE table t1 
INNER JOIN table t2  ON t1.ParcelID = t2.ParcelID
SET t1.Address = IF(t2.Address = '' OR NULL, t2.Address, t1.Address)
WHERE t1.ParcelID = t2.ParcelID;

any suggestions? Is writing the WHERE clause redundant?

Thanks in advance ^^

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

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

发布评论

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

评论(1

野鹿林 2025-02-06 13:48:02

一个选项可以是将条件移动在JOIN和SET ADDEM2内部,如跟随:

UPDATE     tab t1
INNER JOIN tab t2
        ON t1.ParcelID = t2.ParcelID
       AND (t1.Address = '' OR t1.Address IS NULL)
       AND NOT (t2.Address = '' OR t2.Address IS NULL)
SET t1.Address = t2.Address;

Demo 在这里

One option could be moving the condition inside the join and set address2 to address1 directly, as done followingly:

UPDATE     tab t1
INNER JOIN tab t2
        ON t1.ParcelID = t2.ParcelID
       AND (t1.Address = '' OR t1.Address IS NULL)
       AND NOT (t2.Address = '' OR t2.Address IS NULL)
SET t1.Address = t2.Address;

Demo here.

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