MySQL - Update column value based on joined table column value - mysql

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

Related

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;

Updating table using JOIN not working correctly

I'm doing something like this:
UPDATE `widget_list` a
JOIN `geography` b
ON b.`zip_code` = a.`zip_code`
SET b.`msa` = a.`msa`;
This executes just fine, but affects no rows. msa is set to NULL for all rows as if it wasn't changed at all. zip_code and msa are the same data types and length and definitely have overlapping zip_code. Any idea why this won't update?
EDIT: Update...I tried with a different column name and it ran as well, said that a set of my rows were affected, but none actually changed.
EDIT2: If I do this, then my msa column shows NULL for every row. Why wouldn't this value be here for a simple join?
SELECT * FROM `widget_list` a
JOIN `geography` b
ON b.`zip_code` = a.`zip_code`
Something like the below should work.
UPDATE `widget_list`
SET `msa` = (SELECT `msa` FROM `geography` b
JOIN `widget_list` a
ON `a`.`zip_code` = `b`.`zip_code`
);
Able to reproduce using this fiddle
However by changing the order on the set function it works... as in this fiddle
UPDATE `widget_list` a
JOIN `geography` b
ON b.`zip_code` = a.`zip_code`
SET a.`msa`=b.`msa` ;
and again that it should work.. implying it's a setup issue in your enviroment, or we're missing something (trigger perhaps?)
Working Fiddle Note however when mopre than one msa exists per zipcode A, X in my example for zip 123... the system simply picks one and uses it to update. X is lost in my example... are you sure that's not what's happening to you? (it's why I want to see sample data to see if it's a data issue vs syntax vs logic)
updated:
fiddle So if your source table has null values in the zipcodes you'll get null values in the destination table...
so what happens if we add....
UPDATE `widget_list` a
JOIN `geography` b
ON b.`zip_code` = a.`zip_code`
and b.msa is not null
SET a.`msa`=b.`msa` ;
and fiddle showing excluded nulls
Use from to do join.
UPDATE widget_list
SET b.msa = a.msa
FROM widget_list a JOIN geography b ON b.zip_code = a.zip_code

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

How to set a column value equal to the value in another table?

I am trying to figure out how to update a row in one table, setting a column value equal to a value in a different table. Here's an example:
movies:
movie_id | movie_price
movies_attended:
attended_id | attended_movie_id | attended_movie_price
Now, this is kind of a stupid example, but supposed that for some reason there is a row in movies_attended that does not have the correct attended_movies_price in it and so it needs to be updated.
How should a query be written to update the movies_attended table, setting movies_attended.attended_movie_price = movies.movie_price?
I tried something similar to the following, but it did not work:
update movies_attended, movies
set movies_attended.attended_movie_price = movies.movie_price
where movies_attended.attended_movie_id = movies.movie_id
AND attended_id = [the id of the row we want to update]
When you say "it did not work", do you mean that it reported 0 rows updated, or did the statement cause the database raise an exception?
Your example statement appears to be of the form:
UPDATE movies_attended a
JOIN movies m
ON a.attended_movie_id = m.movie_id
SET a.attended_movie_price = m.movie_price
WHERE a.attended_id = ?
(We typically prefer the JOIN ... ON ... style syntax to the comma join operator and the join predicates in the WHERE clause.)
I have no explanation as to why this statement would "not work".
It's possible this would report 0 rows affected, if no rows satisfy the predicates. It would also report 0 rows affected if the rows that would be changed do not require any changes... that is, the existing value in attended_movie_price already matches the value being assigned to it.
Normally, before running an update statement like that, I write it as a SELECT first, and see what values are returned...
By replacing the UPDATE keyword with SELECT ... FROM, and removing the SET clause:
SELECT m.movie_price AS new_val
, a.attended_movie_price AS old_val
, a.attended_id
FROM UPDATE movies_attended a
JOIN movies m
ON a.attended_movie_id = m.movie_id
WHERE a.attended_id = ?
This is actually a bad database design. You don't need movie price in two tables.
But, if you just need this, it goes something along this:
UPDATE movies_attended
INNER JOIN
movies
ON movies_attended.attended_movie_id = movies.movie_id
SET movies_attended.attended_movie_price = movie.movie_price

Update Value in Table from another table

I realized that i was using a varchar attribute as a index/key in a query, and that is killing my query performance. I am trying to look in my precienct table and get the integer ID, and then update my record in the household table with the new int FK, placed in a new column. this is the sql i have written thus far. but i am getting a
Error 1093 You can't specify target table 'voterfile_household' for update in FROM clause, and i am not sure how to fix it.
UPDATE voterfile_household
SET
PrecID = (SELECT voterfile_precienct.ID
FROM voterfile_precienct INNER JOIN voterfile_household
WHERE voterfile_precienct.PREC_ID = voterfile_household.Precnum);
Try:
update voterfile_household h, voterfile_precienct p
set h.PrecID = p.ID
where p.PREC_ID = h.Precnum
Take a look at update reference here.
Similarly, you can use inner join syntax as well.
update voterfile_household h inner join voterfile_precienct p on (h.Precnum = p.PREC_id)
set h.PrecID = p.ID
What if the subquery returns more than one result? That's why it doesn't work.
On SQL Server you can get this type of thing to work if the subquery does "SELECT TOP 1 ...", not sure if mysql will also accept it if you add a "limit 1" to the subquery.
I also think this is pretty much a duplicate of this question ("Can I have an inner SELECT inside of an SQL UPDATE?") from earlier today.
Firstly, your index on a varchar isn't always a bad thing, if it is not a key you can shrink how much of the field you index to only index say the first 10 chars or so.
Secondly, it won't let you do this as if it is a set that is returned it could break.