欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MYSQL替换时间(年月日)字段时分秒不变实例解析

程序员文章站 2023-11-09 20:59:46
写法1: update sas_order_supply_month_pay set receive_time=replace(receive_time,da...

写法1:

update sas_order_supply_month_pay set 
receive_time=replace(receive_time,date_format(receive_time,'%y-%m-%d'),(select period_end from sas_task_supply_month_pay_period where belong='1729' 
and create_time like '%2017-07-12%')) where order_code='po201707130115';

写法2:

update sas_order_supply_month_pay set 
receive_time= addtime ((select period_end from sas_task_supply_month_pay_period where belong='1729' 
and create_time like '%2017-07-12%')+interval 0 hour,time(receive_time)) where order_code='po201707130115';

写法3:

update sas_order_supply_month_pay set 
receive_time = concat((select period_end from sas_task_supply_month_pay_period where belong='1729' 
and create_time like '%2017-07-12%'),' ',date_format(receive_time,'%h:%i:%s')) where order_code='po201707130115';

说明: s

as_order_supply_month_pay表的receive_time字段格式为"2017-06-16 12:13:16",sas_task_supply_month_pay_period表的period_end字段格式为"2017-07-12",

执行后receive_time修改为"2017-07-12 12:13:16"

错误写法:

update sas_order_supply_month_pay set 
receive_time = date_format(concat((select period_end from sas_task_supply_month_pay_period where belong='1729' 
and create_time like '%2017-07-12%'),' ',(select date_format(receive_time,'%h:%i:%s') from sas_order_supply_month_pay 
where order_code='po201707130115')),"yyyy-mm-dd %h:%i:%s") where order_code='po201707130115';

错误写法报错:

[err] 1093 - you can't specify target table 'sas_order_supply_month_pay' for update in from clause

错误分析:

错误语句:

(select date_format(receive_time,'%h:%i:%s') from sas_order_supply_month_pay where order_code='po201707130115')

此语句单独执行是可以的,但是合在一起执行报错,猜测:修改表和子查询不能是同一个表?

以上所述是小编给大家介绍的mysql替换时间(年月日)字段时分秒不变的实现方法,希望对大家有所帮助

上一篇: SpringBoot:处理跨域请求

下一篇: