php CodeIgniter 查询:如何将列值移动到同一行中的另一列并将当前时间保存在原始列中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18691972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter query: How to move a column value to another column in the same row and save the current time in the original column?
提问by chris
In my db table, I have two datetime columns: Last
and Current
. These column allow me to keep track of when someone last used a valid login to the service I am building up.
在我的数据库表中,我有两个日期时间列:Last
和Current
. 这些列允许我跟踪某人上次使用有效登录名登录我正在构建的服务的时间。
Using CodeIgniter's active record, is it possible to update a row so that the Last
value receives the Current
value AND then the Current
value is replace with the current datetime?
使用 CodeIgniter 的活动记录,是否可以更新一行以便该Last
值接收该Current
值,然后将该Current
值替换为当前日期时间?
回答by Rajeev Ranjan
Try like this:
像这样尝试:
$data = array('current_login' => date('Y-m-d H:i:s'));
$this->db->set('last_login', 'current_login', false);
$this->db->where('id', 'some_id');
$this->db->update('login_table', $data);
Pay particular attention to the set()
call's 3rd parameter. false
prevents CodeIgniter from quoting the 2nd parameter -- this allows the value to be treated as a table column and not a string value. For any data that doesn't need to special treatment, you can lump all of those declarations into the $data
array.
特别注意set()
调用的第三个参数。 false
防止 CodeIgniter 引用第二个参数——这允许将值视为表列而不是字符串值。对于不需要特殊处理的任何数据,您可以将所有这些声明集中到$data
数组中。
The query generated by above code:
上述代码生成的查询:
UPDATE `login_table`
SET last_login = current_login, `current_login` = '2018-01-18 15:24:13'
WHERE `id` = 'some_id'
回答by Praveen PL
$data = array(
'name' => $_POST['name'] ,
'groupname' => $_POST['groupname'],
'age' => $_POST['age']
);
$this->db->where('id', $_POST['id']);
$this->db->update('tbl_user', $data);
回答by Mustafiz
if you want to upgrade only a single column of a table row then you can use as following:
如果您只想升级表行的单列,则可以使用以下方法:
$this->db->set('column_header', $value); //value that used to update column
$this->db->where('column_id', $column_id_value); //which row want to upgrade
$this->db->update('table_name'); //table name
回答by mickmackusa
Yes, this is possible and I would like to provide a slight alternative to Rajeev's answer that does not pass a php-generated datetime formatted string to the query.
是的,这是可能的,我想为 Rajeev 的答案提供一个轻微的替代方案,该答案不会将 php 生成的日期时间格式的字符串传递给查询。
The important distinction about how to declare the values to be SET in the UPDATE query is that they must not be quoted as literal strings.
关于如何在 UPDATE 查询中声明要设置的值的重要区别是它们不能作为文字字符串引用。
To prevent CodeIgniter from doing this "favor" automatically, use the set()
method with a third parameter of false
.
要防止 CodeIgniter 自动执行此“偏好”,请使用set()
第三个参数为的方法false
。
$userId = 444;
$this->db->set('Last', 'Current', false);
$this->db->set('Current', 'NOW()', false);
$this->db->where('Id', $userId);
// return $this->db->get_compiled_update('Login'); // uncomment to see the rendered query
$this->db->update('Login');
return $this->db->affected_rows(); // this is expected to return the integer: 1
The generated query (depending on your database adapter) would be like this:
生成的查询(取决于您的数据库适配器)如下所示:
UPDATE `Login` SET Last = Current, Current = NOW() WHERE `Id` = 444
Demonstrated proof that the query works: https://www.db-fiddle.com/f/vcc6PfMcYhDD87wZE5gBtw/0
证明查询有效:https: //www.db-fiddle.com/f/vcc6PfMcYhDD87wZE5gBtw/0
In this case, Last
and Current
ARE MySQL Keywords, but they are not Reserved Keywords, so they don't needto be backtick-wrapped.
在这种情况下,Last
并Current
介绍了MySQL关键词,但他们没有保留关键字,所以他们并不需要是反引号包裹。
If your precise query needs to have properly quoted identifiers (table/column names), then there is always protectIdentifiers().
如果您的精确查询需要正确引用标识符(表/列名称),那么总是有protectionIdentifiers()。