Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Updating all rows setting a field to 0, but setting one row's field to 1

Tags:

php

mysql

Is there an efficient way to update a selection of rows' field to 0, but set One of these rows to 1 based on an ID.

Basically, I have multiple objects in a database, and I want to toggle between which one is "inuse", so the query will set one of the rows (by id) to inuse=1 and the others to inuse=0.

Thanks :)

like image 759
Joel Avatar asked Jun 18 '09 14:06

Joel


People also ask

How do I update a column for all rows in MySQL?

MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.

How update all rows in single column in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

Can you update multiple rows in MySQL?

There are a couple of ways to do it. INSERT INTO students (id, score1, score2) VALUES (1, 5, 8), (2, 10, 8), (3, 8, 3), (4, 10, 7) ON DUPLICATE KEY UPDATE score1 = VALUES(score1), score2 = VALUES(score2);

How do I change a specific value in MySQL?

To replace, use the REPLACE() MySQL function. Since you need to update the table for this, use the UPDATE() function with the SET clause.


1 Answers

UPDATE `table`
SET `inuse` = (`id` = 23)
like image 77
chaos Avatar answered Sep 20 '22 20:09

chaos