I want to update a table row within mysql that doesn't have row id's.
The table name is "unit_status"
unit is_active enabled
17625012 Active 0
I have 2 million of these but want to update this single row to change enabled to "1"
update unit_status
set enabled = 1
where unit = 17625012
and is_active = 'Active'
and enabled = 0
limit 1
You can try:
UPDATE unit_status SET enabled = 1 WHERE unit = 17625012 AND is_active = 'Active' AND enabled = 0
But you should always have a primary key or a unique key.
Related
I have a OpenCart store with more than 23000 products and I need to change one option (Requeres Shipping) for all of my products. I need to change that option in my database. I have a table n0v_product with with column shipping with value 0. I need to change shipping value to 1 for all products. How to update the value in the column with phpMyAdmin?
Run this in a test database before running it on production but this should do the job.
UPDATE n0v_product
SET shipping = '1'
WHERE shipping = '0'
You can use a UPDATE command to change all values on column shipping to 1 on n0v_product:
UPDATE `n0v_product` SET `shipping` = 1
If you only want to set the value if shipping is 0 you can use the following:
UPDATE `n0v_product` SET `shipping` = 1 WHERE `shipping` = 0
You should check the rows before UPDATE to make sure you really want to UPDATE these rows:
-- all rows of table n0v_product
SELECT `shipping`, * FROM `n0v_product`
-- only rows with shipping = 0
SELECT `shipping`, * FROM `n0v_product` WHERE `shipping` = 0
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;
Is there a way in mysql that I can find the number of rows that get locked when a certain query runs? Eg. for a query, what is the number of rows locked:-
UPDATE xyz SET ARCHIVE = 1 , LAST_MODIFIED = CURRENT_TIMESTAMP WHERE ID = '123' AND ARCHIVE = 0;
Assume in this case, there is a index on ID and Archive is part of primary key.
BEGIN;
# lock
UPDATE xyz SET ARCHIVE = 1 , LAST_MODIFIED = CURRENT_TIMESTAMP WHERE ID = '123' AND ARCHIVE = 0;
# returns locked rows (X)
SELECT trx_rows_locked FROM information_schema.innodb_trx;
# release
COMMIT;
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)
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'