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"
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 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'
I now have a MySQL Command for Updating a Row of a MySQL Table with Values from another Table.
update info set info.artist = playlist.artist
from playlist
where info.songname = playlist.songname
There is an Syntax Error. But I can see anything in there.
1st Table
2nd Table
Thanks :)
Try this:
UPDATE info
INNER JOIN playlist ON info.songname = playlist.songname
SET info.artist = playlist.artist
2nd Sample:
UPDATE info
INNER JOIN (
SELECT songname, artist
FROM playlist
WHERE playlist.time_requested IS NOT NULL
ORDER BY playlist.time_requested
LIMIT 1
) PL ON info.songname = PL.songname
SET info.artist = PL.artist
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
I have two tables and both include 2 columns, sureness and kindness and and they are related to each other by dataitemID and id. Just to show you the structure I will put 2 selects that I got from database:
SELECT ID,sureness,kindness FROM omid.tweet ;
SELECT ID,DataitemID,sureness,kindness FROM omid.entity_epoch_data ;
and I want to copy all value of sureness and kindness in omid.tweet into entity_epoch_data where entity_epoch_data.entityID is equal to entityID coming from entity_relation where tweet.ID =entity_relation.ID
I want just to do it in mysql rather than reading the whole table in java and updating the database in the loop but I am so confused. How can I do that?I appreciate any help:)
Update:
I wrote the code as follow but it does not work:
update tweet, entity_epoch_data
set entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness ,
entity_epoch_data.calmness = tweet.calmness ,
entity_epoch_data.happiness = tweet.happiness
WHERE entity_epoch_data.EntityID in(
SELECT EntityID FROM omid.entity_dataitem_relation
INNER JOIN omid.tweet t ON entity_dataitem_relation.DataitemID = t.ID)
It's actually pretty straight forward. The UPDATE clause works like a JOIN and then use SET to set the values
UPDATE tweet INNER JOIN entity_epoch_data
ON tweet.id = entity_epoch_data.id
SET entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness