How to update multiple rows with one query with MySQL? - mysql

I'm currently running more database queries of update, like the following:
UPDATE table SET status = 1 WHERE id = 3
UPDATE table SET status = 1 WHERE id = 7
UPDATE table SET status = 1 WHERE id = 9
UPDATE table SET status = 1 WHERE id = 18
etc...
Question:
How is it possible to run these queries in one?

UPDATE table SET status = 1 WHERE id in (3,7,9,18,...)

If you need to update some rows on a given list you can use IN()
UPDATE table SET status = 1 WHERE id IN (3, 7, 18);
If instead you need to update all rows just don't add any WHERE conditions
UPDATE table SET status = 1;

Your question is a bit general if you mean how to update multiple rows in one command in general it depends on your queries but if your question is more specific and you need to run 1 single query instead of all above queries you can try this :
UPDATE table SET status = 1 WHERE id IN (3,7,9,18)

Related

How to Update query with group by

UPDATE
mine
SET
reason = 'is this possible?',
deleted = 1,
WHERE id = '1'
GROUP BY date
I want to update 2 rows different id but same value, is this possible?
You can update as many rows as you want at the same time. For instance, to update ids 1 and 2:
UPDATE mine
SET reason = 'is this possible?',
deleted = 1
WHERE id IN (1, 2);
In fact, you can leave out the WHERE clause entirely and update all rows in the table.
I'm not sure why you have GROUP BY date, but date doesn't seem relevant to the query.

Can we update two tables in 1 query postgresql

I am using postgres as my database and have 2 tables
accounts
id | name | status
accountOwner
account_id | user_id
I have user_id(accountOwner table) and status(accounts table) to be updated based on accountid I have. Is it possible to update both the tables in 1 query? I tried the following
UPDATE accounts,accountOwner SET accounts.status='active', accountOwner.user_id=3 WHERE accounts.id=accountOwner.account_id AND accountOwner.account_id = 1;
No, you cannot update two tables at once. The documentation says:
UPDATE -- update rows of a table
However, you can use a FROM clause, for example:
UPDATE accounts SET status = 'active'
FROM accountOwners
WHERE accounts.id = accountOwners.account_id
AND accountOwners.account_id = 1;

PHP mysql update multiple row by single query SET='101' where id =1,2,3,7,9

I am trying to update rows in mysql but I have to use for loop for multiple update for single value mysql query is
update table set column1='100' where id =1
update table set column1='100' where id =6
update table set column1='100' where id =14
I am using for loop for running query multiple times with different id, I want to run single query for update all rows. Is that possible?
i want to do something like that
update table set column1='100' where id=1,6,14;
Use IN()
update table
set column1='100'
where id in (1,6,14)
or OR
update table
set column1='100'
where id = 1 OR id = 6 OR id = 14
Use IN() Operator
update table_name SET field_name='101'
where id IN(1,2,7,9)

MySQL update a row but a single field

I update a Table with multiple fields. Now one of the fields may only be updated if another field has a defined value, e.g.:
id | name | image | update
--------------------------------------------------
1 | john | myimage.jpg | 0
2 | ben | yourimage.gif | 1
--------------------------------------------------
Now i walk through all rows and update all fields but the image should only be update if the "update"-flag is set to 1.
If its 0 the existing value should not be overwritten.
Now i tried this:
...
`image` = IF(update = 1, VALUES(`image`),`image`)
...
but its obviously not working because it overwrites the image in every case.
update table
set image = new_value
where update = 1
and id = ?// if you want spacific row, if not ignore this line
If you only want to update the image column Ofer's answer is surely the best. If you'd like to pack the image update into a bigger query, the IF() works as follows:
IF(expression, return this if expression true, return this if expression false)
in your case:
UPDATE table t1
SET
t1.image = IF(t1.update = 1, t1.image, 'new image')
First just fetch the value of update from table by query
Select update from your table where id = 'provide row id'
Then using if else condition by checking value of update fetch fire your update
query
eg.
if($update == 1)
{
echo "Your update query here";
}
else
{
}
Be careful with the name of your column 'update'. It's a reserved word, like you can see below (for updating rows).
I would change it to:
ALTER mytable
CHANGE update update_flag tinyint
and then use the following for updating your rows:
UPDATE mytable
SET image = somevalue
WHERE update_flag = 1
AND id = someid
You only need the last line if you don't want to update all your rows where update_flag is 1.
update your_table
set `image` = case when update = 1
then $newvalue
else `image`
end,
other_column = 'some_value'

MySQL UPDATE Based off of values

I need a query to update rows but first check if there a specific number.
Example:
number = 10
If the row includes the number 10, don't update.
It needs to be just the query. Something like this.
SELECT `number` FROM `users`;
if row.number == 10 then don't update
else update set number=12
UPDATE users
SET number = 12
WHERE number <> 10