用MYSQL替换时间字段且时分秒不变的方法

来源:这里教程网 时间:2026-02-28 10:47:02 作者:
写法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';

说明: sas_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')

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

 

相关推荐