mysql update datetime类型==为空和字符串转换--细节记录

一、mysql数据库里可以直接运行。

update _item_info set TIME_BEGIN=STR_TO_DATE('2022-04-01 17:46:38', '%Y-%m-%d %H:%i:%s'),TIME_END=STR_TO_DATE('2025-05-01 17:46:38', '%Y-%m-%d %H:%i:%s') where TIME_BEGIN is null and TIME_END is null;

  注意用到了

STR_TO_DATE,

1、字符串转换才可以给datetime赋值

2、datetime为空的判断是is null 不是字符串比较

二、C++ Cstring 格式化sql命令时,注意这里的%s是格式符号

		CString strFm = _T("%Y-%m-%d %H:%i:%s");
		sql.Format(L"update _item_info set TIME_BEGIN=STR_TO_DATE('%s','%s'),TIME_END=STR_TO_DATE('%s','%s') where TIME_BEGIN is null and TIME_END is null", strTimeBeg, strFm, strTimeEnd, strFm);

  


直接这样也是可以的,

 

update _item_info set TIME_BEGIN='%s',TIME_END='%s'

 

  当时出错问题是,判断TIME_BEGIN为空,错用了=‘’,应该用is null

 

posted @ 2022-05-27 19:00  曦花  阅读(1461)  评论(0编辑  收藏  举报