Update column with data from another table's column - mysql

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;

Related

How to update values in two tables where one is usermeta MySQL

I have two tables and I need to make single SQL request which will update values in both of them.
Their releation is based on ID (1 table = evdb_users.ID / 2 table = evdb_usermeta.user_id)
For now I did some SQL query like this:
"UPDATE evdb_users
INNER JOIN evdb_usermeta ON evdb_users.ID = evdb_usermeta.user_id AND evdb_usermeta.meta_key='phone_number'
SET evdb_users.user_login='%s', evdb_users.user_email='%s', evdb_users.display_name='%s', evdb_usermeta.meta_value='%s'
WHERE evdb_users.ID=%d"
Data in evdb_users (login/email/display name) - are updating, but nothing changes in usermeta (phone_number)
Please help me to figure out how it should be and how it must works.
try this:
UPDATE evdb_users
INNER JOIN evdb_usermeta
ON evdb_users.ID = evdb_usermeta.user_id
AND evdb_usermeta.meta_key='phone_number'
SET evdb_users.user_login='%s'
, evdb_users.user_email='%s'
, evdb_users.display_name='%s'
, evdb_usermeta.meta_value='%s'
WHERE evdb_users.ID=%d;

SQL Updating entire column based on Primary key from another table

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;

how to update rows with same id and also with conditions?

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'

How to copy 3 columns from one table into another in mysql

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

Update values by another column in another table

I need a query to:
Copy from database player, table item_proto_0, column locale_name
Paste to database player, table item_proto, column locale_name
But:
Just paste if has the same colum vnum
I've tried:
UPDATE item_proto.locale_name
SET item_proto_0.locale_name
WHERE item_proto_0.vnum=item_proto.vnum
Try this:
UPDATE item_proto P1
JOIN item_proto_0 P2
ON P1.vnum = P2.vnum
SET P1.locale_name = P2.locale_name
This should work:
UPDATE item_proto AS ip
INNER JOIN item_proto_0 ip0 ON ip.vnum = ip0.vnum
SET locale_name = ip0.locale_name
Please run this sql query this should do the needful
UPDATE item_proto
inner join item_proto_0 on item_proto.vnum = item_proto_0.vnum
SET item_proto.locale_name = item_proto_0.locale_name