gpt4 book ai didi

mysql - SQLPro 或 PostgreSQL SQL 根据前一行值更新行

转载 作者:行者123 更新时间:2023-11-29 18:04:51 25 4
gpt4 key购买 nike

我无法使用我的 SQL 版本找到当前问题的答案,因此希望有人可以帮助我。

我使用 MySQL 和 SQLPro 作为客户端,或者我可以使用 PostgreSQL pgAdmin 4。

场景我尝试用之前的非空值更新null值。

这是我的 table :

primary_id  name  address  id
1 bob 123 main 100
2 jane 123 main NULL
3 mike 217 2nd 200
4 jeff 217 2nd NULL

enter image description here

如何使用非空值填充null值,以便地址/ID分组在我的列中保持不变?

谢谢!!!

最佳答案

如果你想更新表,这将在 Postgres 中工作:

update t
set id = (select t2.id
from t t2
where t2.address = t.address and t2.id is not null
fetch first 1 row only
)
where id is null;

在 MySQL 中,这将起作用:

update t join
(select address, max(id) as max_id
from t
group by address
) tt
on t.address = tt.address
set t.id = tt.max_id
where t.id is null;

关于mysql - SQLPro 或 PostgreSQL SQL 根据前一行值更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com