I am working on a project where I need to grab the img path from a backup table and insert it in to the image path column of a new table based on if the item names match up perfectly (as the id's vary).
This is what I tried, but getting an error that the items.prod_name column is not found:
MySQL
INSERT INTO items (img_path)
SELECT img_path
FROM items_backup
WHERE items.prod_name = items_backup.prod_name
You would seem to want an update, not an insert. The syntax in MySQL is:
update items i join
items_backup ib
on i.prod_name = ib.prod_name
set i.img_path = ib.img_path;
To follow up on my comment:
UPDATE items
INNER JOIN items_backup ON items.prod_name = items_backup.prod_name
SET items.img_path = items_backup.img_path
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'm not a DB guy and I've looked around here to look for an answer in similar questions but couldn't find one that solves this particular problem:
I have 2 tables - Each with 3 columns (PrimaryKey1, Table1Coln1, Table1Coln2, PrimaryKey2, Table2Coln1, Table2Coln2).
What I'm trying to do is copy values from Table2Coln2 and paste them into Table1Coln2 wherever values in Table1Coln1 & Table2Coln1 are equal. To complicate matters, there are multiple rows with the same values in both corresponding columns in both tables.
I've tried this:
UPDATE Table1
SET Table1.Table1Coln2 = Table2.Table2Coln2
WHERE Table1.Table1Coln1 = Table2.Table2Coln1
When I run the query, it opens a dialog box asking me to enter a value for Table2.Table2Coln2 ?!
Can someone please tell me what I'm doing wrong here?
You should Join Table1 and Table2:
'Clasic' SQL (used in MySQL):
UPDATE Table1, Table2
SET Table1.Table1Coln2 = Table2.Table2Coln2
WHERE Table1.Table1Coln1 = Table2.Table2Coln1
MS Sql (used in Access):
UPDATE Table1 Inner Join Table2 On Table1.Table1Coln1 = Table2.Table2Coln1
SET Table1.Table1Coln2 = Table2.Table2Coln2
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.
I have two tables, one a options table (rot) that has the metrics of a set of rooms and another that has the current state of the rooms (rt). I want to have a daily event that updates the cost_to_date in the rt using the values stored in the rot.
When I tried the SQL:
UPDATE room_tbl SET COST_TO_DATE_rt = COST_TO_DATE_rt + (
SELECT PerDiem_rot FROM room_options_tbl, room_tbl
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`)
I get the error: #1093 - You can't specify target table 'room_tbl' for update in FROM clause
My searching for a solution led me to try a temporary table using aliasing but my attempts at it have all resulted in syntax errors. Any help would be appreciated.
You can't use a query because its not supported by MySQL in an update clause
From 13.2.10. UPDATE Syntax
Currently, you cannot update a table and select from the same table in a subquery.
Instead try the following
UPDATE room_options_tbl, room_tbl
SET COST_TO_DATE_rt = COST_TO_DATE_rt + PerDiem_rot
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`
I have two tables: Item and ItemHistory with a one to many relationship.
I want to flatten the 'item' table a bit and add: dtLastUpdated and nLastUpdatedByUserID columns but I need to upgrade the table data.
Item
nID
dtCreated
dtLastUpdated (new column)
nLastUpdatedByUserID (new column)
ItemHistory
nID
nItemID
dtUpdated
nUpdatedByUserID
Can someone help me to create an update statement to do this. I have tried various things which arent legal in mySQL so am a bit stuck. For example...
UPDATE Item
INNER JOIN ItemHistory ON ItemHistory.nItemID=Item.nID
SET Item.dtLastUpdated = ItemHistory.dtUpdated,
Item.nLastUpdatedByUserID = ItemHistory.nUpdatedByUserID
ORDER BY ItemHistory.dtUpdated DESC
This is required for a mySQL 5.5 installation.
Thanks
update Item as I
inner join (select IH.nItemID,
IH.dtUpdated,
IH.nUpdatedByUserID
from ItemHistory as IH
inner join (select nItemID,
max(dtUpdated) as dtUpdated
from ItemHistory
group by nItemID) as IHLast
on IH.nItemID = IHLast.nItemID and
IH.dtUpdated = IHLast.dtUpdated) as IH
on IH.nItemID = I.nID
set
I.dtLastUpdated = IH.dtUpdated,
I.nLastUpdatedByUserID = IH.nUpdatedByUserID;
The innermost query gets nItemID and max(dtUpdated) to be joined back to ItemHistory to get nUpdatedByUserID for where max(dtUpdated) = dtUpdated. There might be other/better ways of doing this in MySQL but this seams to work as expected.
Dan, your statement will never work because you are updating a column in the item table that doesn't even exist.
According to you, the Item table only has nID, dtCreated and yet you are doing an update on the "dtLastUpdated" column in the Item table which doesn't exist. Similary with the nLastUpdatedByUserID.
See your statement here:
UPDATE Item INNER JOIN ItemHistory ON ItemHistory.nItemID=Item.nID
SET Item.dtLastUpdated=ItemHistory.dtUpdated,
Item.nLastUpdatedByUserID=ItemHistory.nUpdatedByUserID
ORDER BY ItemHistory.dtUpdated DESC
You need to create those columns first in the Item table: Here's the reference manual for doing that in MySQL. Regards.
EDIT: After reading your comment, I am correcting your sql statment (this seems to be the syntax on MySQL for doing updates with inner joins):
UPDATE from Item as I inner join ItemHistory as IH ON IH.nItemID=I.nID
SET I.dtLastUpdated=IH.dtUpdated,
I.nLastUpdatedByUserID=IH.nUpdatedByUserID