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;
Related
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;
I've been reading through SO and other sites, and have followed a few examples; however, my SQL statments is still not performing as required.
I have two tables
parts
============================
pmkParts fnkManufacturer
----------------------------
0 Penn-Union
1 Schneider
2 Telemecanique
and
manufacturer
===============================
Manufacturer pmkManufacturer
-------------------------------
Penn-Union 45
Schneider 56
Telemecanique 12
I want to change the parts table into
parts
============================
pmkParts fnkManufacturer
----------------------------
0 45
1 56
2 12
Here is the SQL statement I tried.
Update parts
SET parts.fnkManufacturer = (
SELECT manufacturer.pmkManufacturer
FROM manufacturer
WHERE manufacturer.pmkManufacturer = parts.fnkManufacturer
)
That is changing the correct column, but it is filling it with 'NULLS' rather than the foreign key (manufacturer). I think there should be a join somewhere in there, but I'm not sure where.
Any tips?
----------
EDIT: Answer:
Here is the SQL statement that worked. Thanks MarcB for the help.
Update parts
SET parts.fnkManufacturer = (
SELECT manufacturer.pmkManufacturer
FROM manufacturer
WHERE manufacturer.Manufacturer= parts.fnkManufacturer
)
Try changing your query like below using a update join query. Again, you are joining on the wrong column, you actually should be joining to manufacturer.Manufacturer column rather.
Update parts p
JOIN manufacturer m ON m.Manufacturer = p.fnkManufacturer
SET p.fnkManufacturer = m.pmkManufacturer;
Your pmkManufacturer looks like int so it is better to add new int field to parts, update it and then remove old column. Something like this.
alter table dbo.parts add pmkManufacturer int
update dbo.parts
set pmkManufacturer = m.pmkManufacturer
from dbo.parts p
inner join dbo.Manufacturer m on p.fnkManufacturer = m.manufacturer
The best solution, you could use INNER JOIN, I for example :
update parts p
inner join manufacturer m on
p.pmkManufacturer = m.Manufacturer
set p.pmkManufacturer = m.pmkManufacturer
Howerver, in your case, if I was wrong, you want to update pmkManufacturer while pmkManufacturer is actually the condition ON for INNER JOIN so I'm not sure that it'okay for request. If not, it isn't also difficult, you could add a new column : pmk_bis_manufacturer and set the value into this column, then delete the old column pmkManufacturer and change the name of new column if nescessary.
One tip for you : the name of columns database, I prefer setting :
pmk_manufacturer instead of pmkManufacturer because capital letter in the name
could make one problem in the futur. For example : for ORM Doctrine,
it isn't good :D
I'm trying to replace null values in one table with values from a second table, based on matches from other columns in both tables. While the code does not result in error, it does not stop running, producing an unending "running query" signal. code is here
UPDATE pl_building b
INNER JOIN pl_grt t
ON b.INST = t.inst
SET b.Utuition=t.tuition
WHERE b.UtUITION = 0;
You should not update on join tables.
I am not sure what field you want to update, but your SQL should look like this:
UPDATE pl_building b
SET b.Utuition= (select t.tuition from pl_grt t ON b.INST = t.inst)
WHERE b.UtUITION = 0;
Make sure :
1) You have an index on t.inst table column and maybe also on b.UtUITION
2) Relationship between b.INST = t.inst is unique. Never returns more than 1 row.
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;
I have a question regarding updating a MySQL database.
I have three tables: Match, Submission and SubmissionVersion. A SubmissionVersion can be set as 'Favorite'. But I can't just query UPDATE SubmissionVersion SET IsFavorite = 1 WHERE ID = $ID because of the relation to a Submission and than the Match. My question is how can I update a SubmissionVersion column with a MySQL Query with two joins? I've tried this query but I can't get it to work.
UPDATE
SubmissionVersion
JOIN
Submission
ON
Submission.ID, SubmissionVersion.SubmissionID
JOIN
Match
ON
Match.ID ON Submission.MatchID
SET
SubmissionVersion.IsFavorite = ".$Index."
WHERE
SubmissionVersion.ID = ".$ID."
AND
Match.ID = ".$MatchID
UPDATE SubmissionVersion sv
SET sv.IsFavorite = ".$Index."
WHERE sv.ID = ".$ID."
AND sv.ID IN (
SELECT s.ID
FROM Submission s
WHERE s.MatchID = ".$MatchID'")
If I understand your statement correctly, that should work.
I eliminated the Match table completely since you're just checking the value against a column in Submission.
Let's start with saying that MATCH is MySQL's reserved word, so needs to be put into backticks.
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html