我在“date”列中有一个空值的表:
platform date id
---------------------------
web 2018-10-10 1
mob 1
mob 1
web 2018-10-15 2
mob 2
ntl 2018-10-09 3
web 2018-10-12 3
web 2018-10-11 4
mob 3我希望通过匹配平台'web‘中的'id’列来更新'mob‘平台的'date’中的空值。结果应该如下所示:
platform date id
---------------------------
web 2018-10-10 1
mob 2018-10-10 1
mob 2018-10-10 1
web 2018-10-15 2
mob 2018-10-15 2
ntl 2018-10-09 3
web 2018-10-12 3
web 2018-10-11 4
mob 2018-10-12 3会很感激你的帮助!
发布于 2018-10-19 05:30:55
UPDATE `table` t1, `table` t2
SET t1.`date` = t2.`date`
WHERE t1.id = t2.id
AND t1.platform = 'mob'
AND t2.platform = 'web'
AND t1.`date` IS NULL
AND t2.`date` IS NOT NULLPS。该解决方案假定在匹配platform = 'web' AND t2.`date` IS NOT NULL条件的表的子数组中,id值是唯一的。否则,将使用可能值列表中的随机值,您必须使用子查询,而不是t2表副本,后者根据platform = 'web'条件筛选数据,按id对数据进行分组,并为每个id值选择单独的data值(例如,最大值)。如果您的MySQL版本是8+,您可以在WITH子句中这样做。
https://dba.stackexchange.com/questions/220529
复制相似问题