I have a big table (addon_values_storage) where some additional product description is stored.
The column container_id represents the product id. Each product can have multiple entries in this table (addon_values_storage). All rows with the same container_id (which is unique) represent the additional description of one product.
No my problem:
I want to update some description with conditions.
- only update within same container_id
- only update when some information exists within this container_id range
I tried something like this but i think this is not right:
UPDATE addon_values_storage
SET addon_value = "TEXT"
WHERE addon_key = "products_pc_tower_detail"
AND EXISTS (SELECT *
FROM addon_values_storage
WHERE addon_values_storage.container_id = addon_values_storage.container_id
AND addon_values_storage.addon_key = 'products_pc_groupid'
AND addon_values_storage.addon_value = 'CL-AM4-iGPU');
This is how the table looks like:
(picute shows only some rows of container_id = 8, but there are many more 1-1100 container_id's each unique id has about 50 rows...)
Does this achieve what you want?
(it should update addon_values for all entries having "products_pc_tower_detail" as addon_key only if there is another entry in the table with the same container_id and with addon_key = 'products_pc_groupid' and addon_value = 'CL-AM4-iGPU')
UPDATE addon_values_storage avs1
JOIN addon_values_storage avs2
ON avs1.container_id = avs2.container_id
AND avs2.addon_key = 'products_pc_groupid'
AND avs2.addon_value = 'CL-AM4-iGPU'
SET avs1.addon_value = "TEXT"
WHERE avs1.addon_key = "products_pc_tower_detail"
Edit: fixed the typo :)
With a self join and the conditions in the WHERE clause:
update addon_values_storage t1
inner join addons_values_storage t2 on t1.container_id = t2.container_id
set t1.addon_value = "TEXT"
where
t1.addon_key = "products_pc_tower_detail"
and t2.addon_key = 'products_pc_groupid'
and t2.addon_value = 'CL-AM4-iGPU'
Related
Example we have 2 tables:
device_tb has columns "device_num" and "device_name"
property_tb has columns "id" and "item_name(currently null or placeholder values)"
device_tb.device_num is equal to the property_tb.id as in it lists the unique id of the product.
If i want to update property_tb.item_name with the strings from device_name instead of manually keying in the names how would i go about it?
Would this work?
UPDATE property_tb
SET item_name= device_tb.device_name
WHERE property_tb.id = device_tb.device_num
You have to join both the tables before updating. Here is the sample code
UPDATE property_tb
SET property_tb.item_name = device_tb.device_name
FROM property_tb
JOIN device_tb ON property_tb.id = device_tb.device_num
In MySQL, the correct syntax for an UPDATE with JOIN is:
UPDATE property_tb p JOIN
device_tb d
ON p.id = d.device_num
SET p.item_name= d.device_name;
I have two tables, one that stores the the id of the an old img
id | old_img_name | old_img_id
and another table that stores the info of the current img
id |img_name | img_w | img_h | etc....
and im trying to create a query to update(switch) the info from the old table into the new one, the only field that I want to update is the img_name.
I have create two queries to do that :
SELECT id, old_img_id, old_img_name FROM `img_archives` WHERE id = 3
UPDATE imgs SET img_name = old_img_name WHERE id = old_img_id
I want to convine these two queries into one, but I'm having a lot or problem doing it.
I have tried this:
UPDATE imgs SET img_name = img_archives_old_img_name FROM imgs INNER JOIN img_archives ON imgs_id = img_archives.old_img_id;
not is not working
UPDATE imgs i INNER JOIN img_archives ia ON i.imgs_id = ia.old_img_id SET i.img_name = ia.old_img_name;
more info is here:
http://www.mysqltutorial.org/mysql-update-join/
-- this should do it
UPDATE imgs
SET img_name = img_archives.old_img_name
FROM imgs
INNER JOIN img_archives ON imgs_id = img_archives.old_img_id;
In mysql the update with join should use this sintax
If you want update the id = 3
UPDATE imgs
INNER JOIN img_archives ON imgs.imgs_id = img_archives.old_img_id
AND img_archives.id = 3
SET img_name = "test"
I need to run an UPDATE query:
UPDATE products
SET fcategory = SELECT fcategory
FROM categories
WHERE categories.scategory = products.scategory
i.e. for the rows in "products" where column scategory = categories.scategory, products.fcategory must be updated to categories.fcategory
Example:
categories.scategory monkies, categories.category gorilla
products.scategory monkies
=> products.fcategory must be updated to gorilla since products.scategory = monkies = products.scategory
Anyone knows how to write such an UPDATE query?
Thanks.
Use a JOIN
UPDATE products AS p
JOIN categories AS c ON p.scategory = c.scategory
SET p.fcategory = c.scategory
Let's say I have two tables in a MySQL database. I would like to update the data from a column in the source table to a column in the target table.
Source table is called 'Computers_F1' Source column is called 'Family'.
Target table is called 'Advice' Target column is called 'Fase1'
I want to put all the rows from 'Family' into 'Fase1'. But this without it adding new rows. I want all the information to be updated in the existing rows.
Hope you guys can help me!
You can do something like this if you have a column id to match both
UPDATE Advice AS a
SET Fase1 = (SELECT Family FROM Computers_F1 AS c WHERE a.id = c.id)
And then rename the column Fase1
ALTER TABLE Active CHANGE `Fase1` `Family` VARCHAR(255)
(replacing VARCHAR(255) with the right format of your column)
I know 2 ways:
UPDATE Advice, Computers_F1
SET Advice.Fase1 = Computers_F1.Family
WHERE Advice.Id = Computers_F1.Id;
OR
UPDATE Advice
INNER JOIN Computers_F1
ON Advice.Id = Computers_F1.Id
SET Advice.Fase1 = Computers_F1.Family;
Hope this helps...
UPDATE
Correct examples, with foreign keys:
UPDATE Advice, Computers_F1
SET Advice.Fase1 = Computers_F1.Family
WHERE Advice.Computers_F1_Id = Computers_F1.Id;
OR
UPDATE Advice, Computers_F1
SET Advice.Fase1 = Computers_F1.Family
WHERE Advice.Id = Computers_F1.Advice_Id;
OR
UPDATE Advice
INNER JOIN Computers_F1
ON Advice.Computers_F1_Id = Computers_F1.Id
SET Advice.Fase1 = Computers_F1.Family;
OR
UPDATE Advice
INNER JOIN Computers_F1
ON Advice.Id = Computers_F1.Advice_Id
SET Advice.Fase1 = Computers_F1.Family;
OK I have 2 tables:
holdings: (id, long_name, value, date, sedol)
asset_key: (id, long_name, sedol)
My issue is that in holdings there are many records where the sedol wasn't filled in. I have the asset_key table however that maps a given long_name to a sedol.
Is there a query that can populate holdings.sedol with the result from asset_key?
Something like:
UPDATE holdings SET holdings.sedol =
SELECT asset_key.sedol FROM asset_key
WHERE sedol.long_name = asset_key.long_name
This will do the trick:
UPDATE
holdings
LEFT JOIN asset_key ON sedol.long_name = asset_key.long_name
SET
holdings.sedol=asset_key.sedol
This should work:
UPDATE `holdings`
SET `holdings`.`sedol` = (SELECT `asset_key`.`sedol`
FROM `asset_key`
WHERE `asset_key`.`long_name` = `holdings`.`long_name`)
However, if I am not wrong, you should be sure that this SELECT subquery returns only one row or MySQL will throw an error.
Try the below Query:
update holdings
SET holdings.sedol = asset_key.sedol
from holdings
inner join asset_key on sedol.long_name = asset_key.long_name
Note: The inner join should result in single value only