Update Multiple Rows with One Query

UPDATE multiple rows with different values in one query in MySQL

You can do it this way:

UPDATE table_users
SET cod_user = (case when user_role = 'student' then '622057'
when user_role = 'assistant' then '2913659'
when user_role = 'admin' then '6160230'
end),
date = '12082014'
WHERE user_role in ('student', 'assistant', 'admin') AND
cod_office = '17389551';

I don't understand your date format. Dates should be stored in the database using native date and time types.

SQL - Update multiple records in one query

Try either multi-table update syntax

UPDATE config t1 JOIN config t2
ON t1.config_name = 'name1' AND t2.config_name = 'name2'
SET t1.config_value = 'value',
t2.config_value = 'value2';

Here is a SQLFiddle demo

or conditional update

UPDATE config
SET config_value = CASE config_name
WHEN 'name1' THEN 'value'
WHEN 'name2' THEN 'value2'
ELSE config_value
END
WHERE config_name IN('name1', 'name2');

Here is a SQLFiddle demo

Update multiple rows with different values in a single SQL query

There's a couple of ways to accomplish this decently efficiently.

First -

If possible, you can do some sort of bulk insert to a temporary table. This depends somewhat on your RDBMS/host language, but at worst this can be accomplished with a simple dynamic SQL (using a VALUES() clause), and then a standard update-from-another-table. Most systems provide utilities for bulk load, though

Second -

And this is somewhat RDBMS dependent as well, you could construct a dynamic update statement. In this case, where the VALUES(...) clause inside the CTE has been created on-the-fly:

WITH Tmp(id, px, py) AS (VALUES(id1, newsPosX1, newPosY1), 
(id2, newsPosX2, newPosY2),
......................... ,
(idN, newsPosXN, newPosYN))

UPDATE TableToUpdate SET posX = (SELECT px
FROM Tmp
WHERE TableToUpdate.id = Tmp.id),
posY = (SELECT py
FROM Tmp
WHERE TableToUpdate.id = Tmp.id)

WHERE id IN (SELECT id
FROM Tmp)

(According to the documentation, this should be valid SQLite syntax, but I can't get it to work in a fiddle)

SQL update multiple rows with different values where they match a value from a list

You could use a case expression:

update mytable
set email = case email
when 'OldEmail@1.com' then 'NewEmail@1.com'
when 'OldEmail@2.com' then 'NewEmail@2.com'
end
where email in ('OldEmail@1.com','OldEmail@2.com')

Or better yet, if you have a large list of values, you might create a table to store them (like myref(old_email, new_email)) and join it in your update query, like so:

update t
set t.email = r.new_email
from mytable t
inner join myref r on r.old_email = t.email

The actual syntax for update/join does vary accross databases - the above SQL Server syntax.

How to update multiple rows at the same time with one query

You can do:

update `Bikeshare_data.2019_bike_data`
set from_station_name = replace(from_station_name, ' (*)', ''),
to_station_name = replace(to_station_name, ' (*)', ''),
where from_station_name like '% (*)' or
to_station_name like '% (*)';

Note: This assumes that ' (*)' does not appear anywhere else in the names. That seems like a reasonable assumption.



Related Topics



Leave a reply



Submit