How to update record using mysql join? - mysql

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;

Related

Merging two MySQL Tables SQL Statement

I'm trying to "merge" two tables and have found a few examples but I'm having difficulty applying them as it continues to say I have syntax error:
UPDATE T2
SET payable_id = T1.payable_id, payable_type = T1.payable_type
FROM payments_distributions AS T2
JOIN payables AS T1
ON T1.payments_distribution_id = T2.id
It mentions that the FROM is at an invalid position at the moment.
I'd appreciate the help. Thanks
Move the SET clause to the end and all of the table references after UPDATE.
UPDATE payments_distributions t2
INNER JOIN payables t1
ON t1.payments_distribution_id = t2.id
SET t2.payable_id = t1.payable_id,
t2.payable_type = t1.payable_type;

Use SELECT result to query another table's row

I have a pretty simple MySQL query to implement, but I can't figure out how...
I have two tables, T1 and T2.
What I need to do:
From T1, I retrieve an ID based on a CODE value:
SELECT id FROM T1 WHERE code = '$code';
Then I need to use this ID (so the value I just retrieved) to update a specific row in T2 (the name of the row will match the ID's value).
I was thinking about using either subqueries or user-defined variables, but no matter how I try it I can't get it done.
If you have any code snippet that can help me doing that, I would appreciate it as well!
EDIT
Just to clarify something: I don't know the name of the column that I need to update in T2, since that name will be the value I retrieve from T1.
So for example, if the ID I get from T1 is "03", it will update the column named "03" in T2.
EDIT 2
Here's a little schema of what I intend to achieve (hoping I make myself clearer, I'm sorry for the misunderstanding...)
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID = (SELECT id FROM T1 WHERE code = '$code')
UPDATE: If the sub query returns more than one row then you can use from IN operator
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID IN (SELECT id FROM T1 WHERE code = '$code')
Use an UPDATE with a JOIN:
UPDATE T2
CROSS JOIN T1
SET T2.`0` = IF(T1.id = 0, T1.someColumn, T2.`0`),
T2.`1` = IF(T1.id = 1, T1.someColumn, T2.`1`),
T2.`2` = IF(T1.id = 1, T1.someColumn, T2.`1`)
WHERE T1.code = '$code'
Replace someColumn with the column in T1 containing the value you want to put into T2.
you can update without using the subquery just using join
update t2
inner join t1 on t2.name = t1.id and t1.code ='$code'
set t2.my_col = 'my_value'
but you should not use var in your query you are at risk for sql injection take a look at you mysql driver for param_binding
UPDATE T2 SET COL = YOUR_VALUE WHERE EXISTS
(SELECT 1 FROM T1 WHERE T2.id=T2.id AND code = '$code')
You can update T2 with the next SQL if you expect at least one row from the inner query
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID IN (SELECT ID FROM T1 WHERE CODE = $code)
Otherwise, if from T1 you are sure you will get only 1 record
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID = (SELECT ID FROM T1 WHERE CODE = $code)

Set column value based on join and self join MySql Rails

I am trying to set a column value in MySQL based on the following query.
select * from table1 t1
join table2 t2
on t1.id = u.t1_id
and t2.status = 'verified'
and not exists (
select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id
and t2_2.updated_at > t2.updated_at
)
This query returns the results I want, but when I try to add
SET t1.column_k = 'some value'
to the end, I'm getting an error that simply says You've got a syntax error near set t1.column_k.... check manual corresponding to your version of MySQL.
I'd really like to know how to include a set on the results of this query and am having trouble formulating that. Any help or ideas?
It's difficult and confusing to me I think because of the self join. The eventual plan is to port this query w/the set command into a migration file in rails once I've got it working.
You need an update. Select isn't used for setting values.
update table1 t1 join
table2 t2
on t1.id = u.t1_id and
t2.status = 'verified' and
not exists (select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id and
t2_2.updated_at > t2.updated_at
)
set t1.column_k = 'some value';

mysql syntax update record based on other record same table

How do i make sql syntax on mysql,
when i want to update parent_id based on id on the other record on the same table with the same value on specific column field, example field Code
i tried to make the following
update product_class t1
set t1.parent_id = t2.id
WHERE t1.family_code <>'' and t1.class_code = ''
join product_class t2
on
(t1.segment_code = t2.segment_code)
but gives me error
Here is the table structure:
Here is the correct syntax:
update product_class t1 join
product_class t2
on t1.segment_code = t2.segment_code
set t1.parent_id = t2.id
where t1.family_code <> '' and t1.class_code = '';
The join is part of the update clause in MySQL.
NOTE: the query doesn't look like it would do the right thing. You are doing a self-join on what looks like a non-unique column, which will generate lots of matches. An arbitrary matching row would then be used for the update.

MySql Query If Field equals Field in different table update different field

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 = ?