问题
In my db I have a Last and a Current column for datetime. Just to keep an eye on when someone last used a valid login to the service I am building up.
What I want to know is, is it possible to update one column on a row with another column from that row, while updating the other column at the same time
Ie: Set the Last as the current Current, then set the Current with the date thats right now essentially?
Make any sense?
回答1:
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);
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'
回答2:
$data = array(
'name' => $_POST['name'] ,
'groupname' => $_POST['groupname'],
'age' => $_POST['age']
);
$this->db->where('id', $_POST['id']);
$this->db->update('tbl_user', $data);
回答3:
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
回答4:
$this->db->set('usage', 'usage'); //Set the column name and which value to set..
$this->db->where('tag', 'java'); //set column_name and value in which row need to update
$this->db->update('tags'); //Set your table name
Try this...
回答5:
$id= $this->input->Post('edit_id');
$date= $this->input->Post('date');
$title=$this->input->Post('title');
$content=$this->input->Post('content');
$value=array('date'=>$date,'content'=>$content,'title'=>$title);
$this->db->where('course_id',$id);
if( $this->db->update('course',$value))
{
return true;
}
else
{
return false;
}
回答6:
I use this function for all updates in my CI sites
function _updateRowWhere($table, $where = array(), $data = array()) {
$this->db->where($where);
$this->db->update($table, $data);
}
回答7:
Alternative Short-hand way to write update query is like below...
$this->db->update('student',array('class'=>'xyz','name'=>'abc'),array('stud_id'=>20);
set class xyz, name abc where stud_id=20 in "student" table..
回答8:
Try the below code:
$data = array(
'name' = > $_POST['name'] ,
'groupname'= > $_POST['groupname'],
'age' = > $_POST['age']
);
$this->db->where('id', $_POST['id']);
$this->db->update('tabname', $data);
来源:https://stackoverflow.com/questions/18691972/codeigniter-single-update-query-to-update-one-column-with-an-existing-columns-da