I would like to know the working of MySQL UPDATE table1 [,tables] SET col1=val1[,cols] query.
Also, how is it different from UPDATE tables with JOIN query?
Try this
UPDATE T1, T2,
[INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1
SET T1.C2 = T2.C2,
T2.C3 = expr
WHERE condition
Related
Hi I have two table one like this one:
table1
and one like this:
table2
I would like to update all the fields on the table2 column "newID" based on this rules: if (table2.ID = table1.ID_actual or table2.ID=table1.ID_old) then table2.newID = table1.newID
How can I resolve this problem ?
You need a join of the 2 tables in the UPDATE statement:
UPDATE table2 t2
INNER JOIN table1 t1 ON t2.ID IN (t1.ID_actual, t1.ID_old)
SET t2.newID = t1.newID
I'm coding queries for a MYSQL database i created. I have a very large amount of rows and i'm asking myself which version of update is faster.
Is faster:
a) Version with WERE
UPDATE table t1 JOIN table t2
SET t1.c1 = 'something'
WHERE t1.c1 = t2.c1
or
b) Version without WERE
UPDATE table t1 JOIN table t2 on t1.c1 = t2.c1
SET t1.c1 = 'something'
?
The question could be also: is it better to specify the "CONDITION" in the JOIN of two tables or in the WERE statement in an update in MYSQL to get faster performance?
I have got two tables. I want to update MODEL in table2 when ITEM in table1 equals ITEM in table2.
Any Ideas?
In MySQL, you do it like this
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.col1 = t2.col1,
t1.col2 = t2.col2
If I understand correctly, you just want to perform an UPDATE on table2 based on, presumably, foreign keys?
If that's right, this should work:
UPDATE
table2
JOIN table1
ON table1.ITEM = table2.ITEM
SET
MODEL = 'new value';
The table declaration in an UPDATE statement is the same as is specified in a SELECT statement - so you can use any type of JOIN that fits your table/data.
Docs for UPDATE, SELECT.
If you could add an actual query attempt, or something, that might be helpful. Can you try something like the following:
UPDATE table2 JOIN table1 ON table2.ITEM = table1.ITEM SET MODEL = ?
The following will get me values from a reference table t2 I want to insert to or to update existing tuple with in table t1:
SELECT
id, col1
FROM
t2
LEFT OUTER JOIN
t1
ON
t2.id=t1.id
If a tuple with id already exist in t1, it should be updated with the value selected from t2. If a tuple with id does not exist in t1, (id, col1) should be inserted with other columns set to default values.
How to do this efficiently?
use this two querys:
This will join and filter, giving you the values that exists in both tables, so you just do the update
Update t1 set t1.col1 = t2.col1
from t1 inner join t2 on t1.id = t2.id
This will join and filter, giving you the values that are in t2, but not in t1, so you just do the insert.
insert into t1 select t2.id, t2.col1
from t2 left outer join t1 on t2.id = t1.id where t1.id IS NULL
UPDATE:
as I can see from here MySQL uses another sintax for this. So you query may work with this instead of the query above:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.id= temp2.id
SET temp1.col1= temp2.col1
but the concepts are the same (just a different syntax)
You won't need the Where, because the INNER JOIN will only use fields that match/join.
In my mysql I am having t1, t2 tables and I want to update t1's field from t2's field value based on t1's field value match t2's field value
I tried the below but it is not updating. What I did wrongly here
UPDATE t1
INNER JOIN t2
ON t1.name = t2.name
SET t1.age = t2.age
WHERE t1.name IS NOT NULL;
You need to separate the table you want to update from the table your querying, even though it is the same:
UPDATE t1
SET t1.age = t2.age
FROM t1 as tempT1
INNER JOIN t2
ON tempT1.name = t2.name
WHERE tempT1.name IS NOT NULL;
UPDATE
Apparently MySQL is using a different UPDATE JOIN Syntax than other db's. Your initial query seems to use the correct syntax, just to be sure try to alias the table names:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.name = temp2.name
SET temp1.age = temp2.age
WHERE temp1.name IS NOT NULL;
UPDATE 2
After looking at this a bit longer I'm certain that the WHERE clause is the issue:
WHERE temp1.name IS NOT NULL
You cannot join on null values anyway, so they are filtered out by default. The WHERE clause is somehow interfering with the join.
Try and remove it to see if the UPDATEworks. If you don' t want to execute and update right away simply execute a select with the same JOIN CLAUSE to see which records would be affected.
Here is a general reference to NULL and JOIN:
http://databases.about.com/library/weekly/aa051203a.htm
Here is the SQL Server Reference in compliance with the above: http://msdn.microsoft.com/en-us/library/ms190409.aspx
Could not find a MySQL reference that states this explicitly but I think this is true for all Relational DBs.
I myself also found another way to achieve this same scenario. I am pasting the answer here so that others will get benefit.
UPDATE t1 SET t2_age = (SELECT age FROM t2 BINARY WHERE name = t1.name);
Try this:
update t1
set t1.age = t2.age
from t1
inner join t2 on t1.name = t2.name
where t1.name is not null
Try this
UPDATE t1,t2
SET t1.age = t2.age
FROM t1 as tempT1
INNER JOIN t2 ON tempT1.name = t2.name
WHERE tempT1.name IS NOT NULL;