I am trying to update a row called ctcode in a table called partnumber copying values from the row ctcode in a table called families. My SQL statement however returns and "Unknown Column 'families.parent' in 'where clause'" error.
Here is my SQL Statement
UPDATE `partnumber`
SET `partnumber`.`ctcode`=`families`.`ctcode`
WHERE `partnumber`.`partnumber`=`families`.`parent`;
What is wrong with my statement? Is there any more efficient way of doing this?
You can basically join both tables even in UPDATE statements,
UPDATE `partnumber` a INNER JOIN `families` b
ON a.`partnumber` = b.`parent`
SET a.`ctcode`= b.`ctcode`
Works in MySQL 5.5.24-0ubuntu0.12.04.1
UPDATE `partnumber`, `families`
SET `partnumber`.`ctcode`=`families`.`ctcode`
WHERE `partnumber`.`partnumber`=`families`.`parent`;
Try this query
UPDATE partnumber
SET ctcode = f.ctcode
FROM partnumber p
INNER JOIN families f ON p.partnumber = f.parent
Related
I have two simple databases, 'rawdosingjune' and 'Dosage_June'.
The following SELECT statement returns the correct data:
SELECT * FROM rawdosingjune WHERE rawdosingjune.corrosion = 'TRUE';
However, if I put the same WHERE condition into an UPDATE statement I get an error?
UPDATE Dosage_June SET Corrosion_Basis = rawdosingjune.DosageBasis
WHERE rawdosingjune.corrosion = 'TRUE';
Error code: 1054. unknown column 'rawdosingjune.corrosion' in 'where clause'
I have tried a number of combinations including creating the tables again?
These are the two tables
rawdosingjune:
Dosage_June:
Judging from your tables, Dosage_June.wellname & rawdosingjune.well is the unique link therefore the INNER JOIN can use it for their ON condition.
Something like this should work:
UPDATE Dosage_June INNER JOIN rawdosingjune
ON Dosage_June.wellname=rawdosingjune.well
SET Dosage_June.Corrosion_Basis = rawdosingjune.DosageBasis
WHERE rawdosingjune.corrosion = 'TRUE';
It should not be surprising that trying to use a WHERE clause that names a table not selected by the query, will not work.
In the case of your SELECT, it's valid, because you name a row that's part of the query.
In the case of your UPDATE, it's a completely unrelated row that the engine has no idea what to do with.
Instead, name a row that's actually part of the table(s) you're operating on.
I have two tables name activties and post_media now I want to update the media background color in activities table according to post media table record but when I run query it give me error.
Query
UPDATE A
SET A.bg_color = M.bg_color
FROM activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
WHERE A.relation_id>0
Error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'FROM activities A INNER JOIN post_media M ON A.relation_id =
M.user_post_' at line 3
UPDATE syntax is different from SELECT. There is no FROM clause usage in UPDATE statement.
General flow is: UPDATE <table name> [JOIN <other tables>] SET ...
UPDATE activities A
INNER JOIN post_media M ON A.relation_id = M.user_post_id AND A.media=M.file
SET A.bg_color = M.bg_color
WHERE A.relation_id>0
Check documentation here for full syntax and further understanding: https://dev.mysql.com/doc/refman/8.0/en/update.html
Update query with use of join is different than SELECT query. Here you need to add tables before SET clause and all conditions in WHERE clause like SELECT.
e.g/
UPDATE t1, t2
SET t1.field = t2.field
WHERE condition 1
AND condition 2
So your query will be like as below:
UPDATE activities A, post_media M
SET A.bg_color = M.bg_color
WHERE A.relation_id = M.user_post_id
AND A.media=M.file
AND A.relation_id>0
Try this one.
I am trying to update the SUIDMatches column of table1_orig_namesplits table based on number of matches it find in the master_table using the following update statement.
But I am getting an "Error Code: 1054. Unknown column 'al.source_id' in 'on clause'" error.
UPDATE test.table1_orig_namesplits al
SET al.SUIDMatches = (
SELECT count(distinct v.SUID)
FROM test.master_table v
JOIN test.table1_orig a ON al.source_id = a.id_names
WHERE al.name_first = v.FirstName
AND al.name_last = v.LastName
AND a.date_of_birth_dt = v.BirthDate_dt );
There is a source_id column in the table1_orig_namesplits table is matched with the id_names column of table1_orig.
Can anyone please suggest how to fix this update statement?
The problem appears to be that the sub-query attempts to join test.table1_orig to test.master_table using a column from test.table1_orig_namesplits which is not part of the join.
For further testing / analysis some sample data would be helpful, but the basic idea is that you'll either have to join to al in the subquery, or take a different approach.
I have 2 tables:
original {ID, FirstName, LastName}
and
dummy {ID(NULL), FirstName, LastName}
I have to insert into t2.ID the original.ID but only if the FirstName and LastName from both tables are the same. Now, i've tried:
1.Error Code: 1054. Unknown column 't2.FirstName' in 'where clause'
INSERT INTO dummy (ID)
SELECT ID
FROM original
WHERE dummy.FirstName = original.FirstName
AND dummy.LastName = original.LastName;
2.Error Code: 1054. Unknown column 'original.FirstName' in 'where clause'
UPDATE dummy
SET ID = original.ID
WHERE dummy.FirstName=original.FirstName
AND dummy.LastName= original.LastName;
3.Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
NOTE: I have to find a way without disableing safe mode.
UPDATE dummy
JOIN original
ON original.FirstName = dummy.FirstName
AND original.LastName = dummy.LastName
SET dummy.IDPacient = original.ID
WHERE original.ID <> 0;
Now if someone could help me understand what i did wrong in each of these 3 cases and/or give me a better solution, i would much appreciate the help.
Version 1 is just plain wrong - you'll insert a new row, not update the existing row.
Version 2 is close, you just need a join to "original":
UPDATE dummy
SET ID = original.ID
FROM dummy
INNER JOIN original
ON dummy.FirstName =original.FirstName
AND dummy.LastName = original.LastName;
You need to perform a join on first and last name between the "original" and "dummy" tables and then update the ID
Try this..
Update a
Set a.ID = b.ID
From dummy a
Join original b
On a.firstname = b.firstname
And b.lastname = b.firstname
You were trying to filter based on columns in the "original" table but it wasn't included in a from clause in your query.
This link might also have some more useful info for you if you need it.
SQL update query using joins
I have a subquery as follows : which will select the id's according to the condition first and delete
the records ,
Delete from post_master_user_map WHERE id IN
(SELECT id FROM `post_master_user_map` WHERE posted_by_user_id=110);
But it gives me the following error :
You can't specify target table 'post_master_user_map' for update in FROM clause
What is wrong with this ? thanks in advance .
UPDATE
This also fails , I dont why
DELETE FROM `post_master_user_map` WHERE `reshare_id` in (SELECT id FROM `post_master_user_map` WHERE
posted_by_user_id=110);
This error occurs when you try to modify a table and select from the same table in sub query.
Anyway to solve that error change your query as follows
Delete from post_master_user_map WHERE posted_by_user_id=110;
For the updated query(in your question) use following
DELETE t1 FROM post_master_user_map as t1 INNER JOIN
post_master_user_map as t2 ON t1.reshare_id=t2.id and t2.posted_by_user_id=110
By this MySQL DELETE FROM with subquery as condition : MySQL does not allow the table you're deleting from be used in a subquery for the condition.