You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何匹配transmitted_profile_fk,用表自身数据更新空datetime列?

解决方案

假设你的表名为profile_table,需要更新的空日期列是datetime_column,匹配字段为transmitted_profile_fk,可以用以下两种方式实现:

方式一:自连接更新(适合同组仅一个非空日期值)

UPDATE profile_table t1
JOIN profile_table t2 
  ON t1.transmitted_profile_fk = t2.transmitted_profile_fk
SET t1.datetime_column = t2.datetime_column
WHERE t1.datetime_column IS NULL
  AND t2.datetime_column IS NOT NULL;

方式二:子查询指定取值规则(适合同组多个非空日期值)

如果同个transmitted_profile_fk下有多个非空日期,可通过MAX()或MIN()指定取最晚/最早的日期:

UPDATE profile_table t1
SET t1.datetime_column = (
  SELECT MAX(t2.datetime_column)
  FROM profile_table t2
  WHERE t2.transmitted_profile_fk = t1.transmitted_profile_fk
    AND t2.datetime_column IS NOT NULL
)
WHERE t1.datetime_column IS NULL;

注意:如果某个transmitted_profile_fk下没有任何非空日期记录,对应的空值行不会被更新。

内容的提问来源于stack exchange,提问作者JanW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:05:56