我需要用其他列中的数据填充DATETIME列,其中一个用于日期,另一个用于时间。
这是我编写的代码的一部分,但是我只能将它们实际结合在一起:
谢谢! 分隔符$
创建触发器完整日期
before insert
on Tabela_veche
for each row
开始
if new.fulldate is null then
set new.fulldate = = CONCAT();
end if;
结束$
答案 0 :(得分:0)
假设您的桌子看起来像这样
+----------+----------+------+-----+---------+---------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+---------+
| dt | date | YES | | NULL | |
| tm | time | YES | | NULL | |
| fulldttm | datetime | YES | | NULL | |
| gcdttm | datetime | YES | | NULL | VIRTUAL |
+----------+----------+------+-----+---------+---------+
4 rows in set (0.02 sec)
and dt is formatted '2019-01-01' and time '10:00:00' then str_to_date
select str_to_date(concat(dt,' ',tm),'%Y-%m-%d %h:%m:%s') dttm
from t;
返回
+---------------------+
| dttm |
+---------------------+
| 2019-00-01 10:00:00 |
+---------------------+
1 row in set (0.00 sec)
@Raymond Nijland指出,您也许可以使用生成的列
`gcdttm` datetime AS (str_to_date(concat(dt,' ',tm) ,'%Y-%m-%d %h:%m:%s'))
并且完全不理会触发器。
+------------+----------+----------+---------------------+
| dt | tm | fulldttm | gcdttm |
+------------+----------+----------+---------------------+
| 2019-01-01 | 10:00:00 | NULL | 2019-00-01 10:00:00 |
+------------+----------+----------+---------------------+
1 row in set (0.00 sec)