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;
Related
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';
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.
Basically I have an update statement which needs to update two fields of a table but is dependent on its where clause which references other tables within the database.
For example.
UPDATE TABLE_ONE
SET VALUE_ONE=1,VALUE_TWO=2
WHERE TABLE_TWO.ID = 1818 AND TABLE_TWO.POSITION = TABLE_THREE.ID AND TABLE_ONE = TABLE_THREE.VALUE = TABLE_ONE.ID;
My question is how do I do this successfully. At the moment I get unknown column exception on the first parameter of the where clause.
I hope this is clear. Any help would be greatly appreciated.
It's hard to tell without seeing table schema for your all tables but you can try to rewrite your update like this
UPDATE table_one t1 JOIN table_three t3
ON t1.id = t3.value JOIN table_two t2
ON t3.id = t2.position
SET t1.value_one = 1, value_two = 2
WHERE t2.id = 1818
Edit Turns out there is an H2 database sitting on top of the mysql database. The query I write hits the H2 instead. I'll keep researching to see if this will work
I have two tables I would like to update at the same time, and the values from one of them is determined by values stored in the other. My update query looks like this:
UPDATE table1 AS A INNER JOIN table2 AS B
ON A.COL1 = B.COL1
AND A.COL2 = B.COL2
SET A.COL3 = 'SOME VAL',
B.COL4 = B.COL4 - A.COL4,
B.COL5 = B.COL5 - A.COL4
WHERE A.ID IN (23,5,21)
I'm getting a syntax error that says 'Expected "SET"' right where I'm doing the INNER JOIN.
I believe I should be able to do this join update per UPDATE multiple tables in MySQL using LEFT JOIN and http://dev.mysql.com/doc/refman/5.0/en/update.html. Does anybody know what my syntax error is?
Update for posterity
First, thanks to Thomas Mueller for his help.
I ended up using the following syntax and as I found it somewhat confusing, I'm leaving it here for future viewers.
UPDATE TABLE1 SET(COL1, COL2) =
( SELECT T1.COL1 - T2.AMNT, T1.COL2 + T2.AMNT
FROM TABLE1 T1 RIGHT JOIN TABLE2 T2
ON T1.COL3 = T2.COL3
AND T1.COL4 = T2.COL4
WHERE T2.ID = 23)
WHERE EXISTS
( SELECT *
FROM TABLE2
WHERE TABLE1.COL3 = TABLE2.COL3
AND TABLE1.COL4 = TABLE2.COL4
AND TABLE2.ID = 23)
Note: I had to use a join in the first select as I couldn't get the syntax we discussed below to work.
As a result of using this method, if I get a list of table2 ids (23,5,21 in my original example) I have to do multiple update statements. If anybody knows a better way to do this, please let me know.
H2 does not support updating two tables at the same time within one SQL statement. You would need to use two statements. For the supported syntax, see the UPDATE statement railroad diagram.
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;