Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrease column value via update query with join clause using CI's active record [duplicate]

I have two tables:

table1: id, user_id, poll_id, options_id

table2: id, poll_id, votes

The column votes is an integer and I want to change the value by joining the tables with some where clauses:

$this->db
->set('votes', 'votes - 1', FALSE)
->join('table1', 'poll_votes.options_id = table2.id')
->where('poll_id', $row)
->where('user_id', $id)
->update('table2');

I'm getting this error:

Error Number: 1054
Unknown column 'user_id' in 'where clause'
UPDATE `table2` SET votes = votes - 1 WHERE `poll_id` = '9' AND `user_id` = '1'
like image 382
user2634127 Avatar asked Feb 03 '26 01:02

user2634127


1 Answers

Try this one:

$this->db->set('votes', 'votes - 1', FALSE)
$this->db->where('table1.user_id',$id);
$this->db->where('table2.poll_id',$row);
$this->db->update('table1 join table2 ON table1.poll_id= table2.poll_id');
like image 192
M Khalid Junaid Avatar answered Feb 05 '26 14:02

M Khalid Junaid