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;
Related
I need to join two tables (TableCorrected) with (TableOriginal). Both tables have columns for ID-number and Articlenumber. Both tables also have a column named "Quantity".
I want to join the two tables, match ID-number AND Articlenumber and update the Quantity-value from "TableOriginal" to "TableCorrected" ONLY where ID-number as well as Articlenumber matches.
I've started a statement as below - but I'm sure it's not correct, returns 0 result.
UPDATE TableOriginal
INNER JOIN TableCorrected ON TableOriginal.ID = TableCorrected.ID
SET TableOriginal.Quantity = TableCorrected.Quantity
WHERE TableCorrected.ID = TableOriginal.ID
AND TableCorrected.Article = TableOriginal.Article
but I'm sure it's not correct
It is correct. But not optimal. More clear is, for example,
UPDATE TableOriginal
INNER JOIN TableCorrected USING (ID, Article)
SET TableOriginal.Quantity = TableCorrected.Quantity;
or
UPDATE TableOriginal
INNER JOIN TableCorrected ON TableOriginal.ID = TableCorrected.ID
AND TableOriginal.Article = TableCorrected.Article
SET TableOriginal.Quantity = TableCorrected.Quantity;
returns 0 result.
UPDATE does not return rows. Rather than SELECT.
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 database with several tables and all my queries work but I would like to had a new one and can't succeed to make it working.
Two tables 'vins' and 'producteurs'with a foreign key IDproducteur in my 'vins' table.
I need to update the 'vins'table from an excel source where the data are not always written corectly and need to change the stock value for some records of the 'vins' table.The p.nomproducteur and V.annee and v.nom are parameters but for test I put hard values.
My Query is the following:
UPDATE vins AS v
JOIN producteurs p
ON ( p.IDproducteur = v.IDproducteur AND p.nomproducteur LIKE "Charles" )
SET stock = 10
WHERE v.nom LIKE "Symphonie" AND v.annee = 2013
I have two records where the producteur is "Charles" and V.nom is Symphonie one withe annee = 2010 and one with 2013. I got no error in phpMyadmin but there is no results to my Query even by changing some command order.
Is what I want impossible to do?.
Put the condition p.nomproducteur LIKE "Charles" in the WHERE clause:
UPDATE vins v
JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET stock = 10
WHERE
v.nom = "Symphonie" AND v.annee = 2013 AND p.nomproducteur = "Charles"
Also there is no need for LIKE in this case, a simple = will do.
The update based on JOIN is commonly used in mysql so be sure that your join and where condition really match the values in your tables
UPDATE vins AS v
INNER JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET v.stock = 10
WHERE v.nom = "Symphonie"
AND v.annee = 2013
AND p.nomproducteur = "Charles"
and do the fact you are not filter for partial string use = instead of like
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
I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"