if statement - MySQL UPDATE using IF to select which column to +1 -
i have vote table, , if post value 1 update tablea column_a +1 else update tablea column_b +1
and here query, no thought how utilize if statement here.
update votes, topics set votes.yesno = 1, if(votes.yesno = 1, topics.num_yes = topics.num_yes+1, topics.num_no = topics.num_no+1) votes.user_id=? , votes.topic_id=? , topics.id = votes.topic_id
i think may you're looking for. can't utilize if specify column, can utilize specify value:
update votes v inner bring together topics t on v.topic_id = t.topic_id set v.yesno = 1, t.num_yes = if(v.yesno = 1, t.num_yes + 1, t.num_yes), t.num_no = if(v.yesno = 1, t.num_no, t.num_no + 1) v.user_id = ? , v.topic_id = ?
and here sample sql fiddle.
good luck.
mysql if-statement sql-update
No comments:
Post a Comment