MYSQL替换时间(年月日)字段,时分秒不变

2018-06-17 23:55:31来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

写法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')

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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:MYSQL LOGBIN 数据日志恢复数据库随笔

下一篇:CentOS6.4将MySQL5.1升级至5.5.36