繁体   English   中英

CodeIgniter:如何使用活动记录将一个列值移动到另一个具有 Where 条件的值

[英]CodeIgniter: How to move one column value to another with Where condition using active record

我想将一个列值移动到另一个列值,例如有两个表列'Location','Call_Location' 我想将'Call_location' 的所有值移动到'Location' 列,条件为仅选定日期范围。 如何在 codeigniter 中做到这一点。

$text = "UPDATE phone_acc SET location = call_location WHERE dt >= 
dateformat(current_date, '%Y-%m-01')
AND dt < dateformat(current_date, '%Y-%m-01') + interval 1 month;
$this->db->query($text);

这是行不通的。

其中一个问题发现它将第二个字段名称作为字符串值“call_type”,而它应该将其作为字段名称。

    UPDATE `phone_acc` SET `location` = 'call_type' WHERE `call_date` = '2020-11- 
   01 01:33:51'

您的查询缺少当月的条件。 假设日期存储在dt列中,您将执行以下操作:

UPDATE phone_acc 
SET location = call_localtion 
WHERE id = 50959757 AND dt >= dateformat(current_date, '%Y-%m-01')

对于满足where谓词的每一行,这会将一个值从一列复制到同一行上的另一个值。

如果您的数据中有未来日期,也请使用上限:

dt >= dateformat(current_date, '%Y-%m-01')
AND dt < dateformat(current_date, '%Y-%m-01') + interval 1 month

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM