我尝试使用date2,date3,date4列中的最高日期更新列date1。 我有选择陈述
SELECT GREATEST(
COALESCE(date2, date3, date4),
COALESCE(date3, date4, date2),
COALESCE(date4, date2, date3)
)
FROM (
SELECT date1,date2,date3,date4
FROM my_table
WHERE date1 is not null and (date1 < date2 or date1 < date3 or date1 < date4);
但我不知道如何使用更新或合并?
+------------------+------------------+------------------+------------------+
| date1 | date2 | date3 | date4 |
+------------------+------------------+------------------+------------------+
| 2017-04-13 16:54 | 2017-04-13 16:57 | | 2016-06-16 |
| 2017-04-13 15:41 | | 2017-04-13 15:42 | 2016-06-16 |
| 2017-04-13 15:43 | 2018-01-10 14:23 | 2017-04-13 15:41 | |
| 2017-04-13 16:05 | | 2017-04-13 16:05 | 2016-06-16 |
| 2017-04-13 16:43 | 2017-04-13 16:43 | | 2016-06-16 |
| 2017-04-13 16:52 | 2017-04-13 16:52 | 2017-04-13 16:07 | 2017-04-17 16:07 |
| 2018-01-10 14:20 | | 2018-01-10 14:23 | |
| 2017-09-27 14:54 | 2015-09-08 09:56 | 2017-09-27 14:54 | 2015-03-13 |
| 2017-06-16 13:38 | | 2017-06-16 13:39 | |
+------------------+------------------+------------------+------------------+
更新/合并后,我希望有类似的东西
+------------------+------------------+------------------+------------------+
| date1 | date2 | date3 | date4 |
+------------------+------------------+------------------+------------------+
| 2017-04-13 16:57 | 2017-04-13 16:57 | | 2016-06-16 |
| 2017-04-13 15:42 | | 2017-04-13 15:42 | 2016-06-16 |
| 2018-01-10 14:23 | 2018-01-10 14:23 | 2017-04-13 15:41 | |
| 2017-04-13 16:05 | | 2017-04-13 16:05 | 2016-06-16 |
| 2017-04-13 16:43 | 2017-04-13 16:43 | | 2016-06-16 |
| 2017-04-17 16:07 | 2017-04-13 16:52 | 2017-04-13 16:07 | 2017-04-17 16:07 |
| 2018-01-10 14:23 | | 2018-01-10 14:23 | |
| 2017-09-27 14:54 | 2015-09-08 09:56 | 2017-09-27 14:54 | 2015-03-13 |
| 2017-06-16 13:39 | | 2017-06-16 13:39 | |
+------------------+------------------+------------------+------------------+
答案 0 :(得分:1)
你应该可以这样做:
gets()
在您的数据中,update t
set date1 = greatest(date1, coalesce(date2, date1), coalesce(date3, date1), coalesce(date4, date1))
where date1 < greatest(date1, coalesce(date2, date1), coalesce(date3, date1), coalesce(date4, date1));
似乎永远不会是date1
。