How to update two tables in one statement? - mysql

UPDATE table1, tmpList
SET table1.ts = tmpList.ts_value
WHERE table1.id = tmpList.id
UPDATE table2, tmpList
SET table2.ts = tmpList.ts_value
WHERE table2.id = tmpList.id
I'm using MySQL

Assuming every id appears in both tables (ideally only once):
update tmpList inner join table1 using (id) inner join table2 using (id)
set table1.ts = tmpList.ts_value, table2.ts=tmpList.ts_value;
Update: simply using left joins instead of inner joins makes this work even for ids that are only in one table - the inapplicable set clause seems to just be skipped; I should have tried it earlier, apparently.

http://dev.mysql.com/doc/refman/5.0/en/update.html

Why do you need one statement? Are you worried about one completing and the other failing, leaving a half-updated mess?
Id that's the case, transactions are what you need. eg.
begin work;
UPDATE table1, tmpList
SET table1.ts = tmpList.ts_value
WHERE table1.id = tmpList.id;
UPDATE table2, tmpList
SET table2.ts = tmpList.ts_value
WHERE table2.id = tmpList.id;
commit work;
You should be able to run all of the above in a single command, but if not, it's safe to run them separately. The data won't be permanently saved unless the commit completes, which only happens if all the previous lines between the begin...commit succeed.

no, this is not possible if you are using simple queries. I am not familiar with my sql but in oracle one cannot update two tables at a time.

Related

H2 update with join

As development DB I am using MySQL, and for tests I am using H2 database.
The following script works in MySQL very well, but it is fails on H2.
UPDATE `table_a`
JOIN `table_b` ON `table_a`.id=`table_b`.a_id
SET `table_a`.b_id=`table_b`.id
In the internet I found that h2 doesn't support UPDATE clause with JOIN. Maybe there is a way to rewrite this script without JOIN clause?
By the way, I am using liquibase. Maybe I can write UPDATE clause with it's xml language?
I tried the following script
UPDATE table_a, table_b
SET table_a.b_id = table_b.id
WHERE table_a.id = table_b.a_id
But I still getting errors. Seems, that H2 doesn't support updating multiple tables in one query. How can I rewrite this query in two different queries to collect ids and insert them?
Try something like this:
update table_a a
set a.b_id = (select b.id from table_b b where b.a_id = a.id)
where exists
(select * from table_b b where b.a_id = a.id)
I've spend a lot of time for this kind of UPDATE. Please find out my comment, maybe somebody find it usefull:
For every rows in WHERE condition executed UPDATE for SET
In inner SELECT you can use updated table columns
In case of error "Scalar subquery contains more than one row" - UPDATE for SET return more, than one row. Problem rows could be found with replace UPDATE by SELECT COUNT(*)
See also Scalar subquery contains more than one row
Sample SELECT WITH UPDATE:
UPDATE USER_DETAILS UD SET UD.GRADUATE_COMMENT=
(SELECT U.COMMENT FROM USERS U WHERE u.ID=UD.id) <-- ref to outer updated table
WHERE UD.GRADUATE_COMMENT IS NULL;

Updating a column from another column conditioned on related table

I've run into a problem where I'd like to copy data between columns based on a condition from a related table. Looking at the top answer from eglasius on this similar problem similar problem I came up with this solution:
UPDATE table1 SET table1.column2 = table2.column1
FROM table1 NATURAL JOIN table2
WHERE table2.column1 = "myCondition"
This query gave me a syntax error beginning at FROM although replacing the UPDATE clause with a SELECT seemed to yield no problems.
It seems that in the case of an UPDATE mySQL appears to dislike a FROM syntax. I had good success moving the join to the front of the query, following it with the JOIN and finally the WHERE condition, like this:
UPDATE table1 NATURAL JOIN table2
SET table1.column2 = table1.column1
WHERE table2.column1 = "myCondition"

How to UPDATE in one table in SQL based on matching data in another table?

I found several questions with similar wording, but none addressed the specific question I have.
How does one perform an UPDATE with conditions that operate between two unlinked tables?
As example
TABLE_I
ID, Placed, junk, junk, junk
TABLE_II
ID, Category, Placed, Note, junk, junk...
If the Condition is in TABLE_II
WHERE Category=9 AND Note=#testvalue
An UPDATE should take place where a value in TABLE_II matches one in TABLE_I
UPDATE TABLE_I SET Placed=#testvalue WHERE
.. the Current TABLE_I.Placed=Table_II.Placed assuming the above conditions are met
Is such stepped-in conditioning even possible in SQL? Or would it require coding outside of the query to test in steps?
SQL
update t1 SET t1.Placed=#testvalue
from Table_1 t1
join Table_2 t2 on t1.placed = t2.placed
where t2.Category=9 AND t2.Note=#testvalue
you have to use join in the update statement
Mysql
the answer is yes you can
try it like that
update Table_1 t1
join Table_2 t2 on t1.placed = t2.placed
where t2.Category=9 AND t2.Note=#testvalue
SET t1.Placed=#testvalue
EDIT:
For general Update join :
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
You could do this kind of stuff with a trigger on insert on table_i combined with a procedure for updating the first table.
Not sure on how to do it in MySQL or SQL-server (why 2 tags? Which is it?), but it probably is not much different from doing this in PostgreSQL. A quick Google search gave me http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
Though the best solution probably is to actually link the unlinked tables.

Update all mysql records from one table into another

Table1 contains all the fields from table2. I need to update table1 with the all the records from table2.
I found this:
UPDATE
table1
INNER JOIN
table2 ON (table2.id = table1.id)
SET
table1.field1 = table2.field1,
table1.field2 = table2.field2;
But I have too many fields and this would take forever to write. How can I update all the fields from table2 into table1? I canĀ“t seem to find the answer, please help.
I'm not terribly familiar with MySQL, but if you can get a list of column names, perhaps with:
SHOW COLUMNS FROM mytable FROM mydb
Then you can paste those into Excel and build your query, just paste your field names in column A , throw this in B1:
="table1."&A1&" = table2."&A1&","
And copy down.

update a table using inner join syntax in mysql

i am curious about how updating a table with inner join works. if i run the following statement:
update tbl1 a
inner join tbl2 b using (id)
set a.val = b.val;
what happens to the records in tbl1 that do not have a match in tbl2? will they simply not be updated and left as is in tbl1? will they be deleted?
i realize i can run this and get the answer but i'm also interested in the mechanics of how this works behind the scenes and was hoping somebody could elucidate this for me.
The update statement is operating on tbl1.
The join is providing a filter that specifies which rows to update in tbl1.
As an added bonus, the join is also providing a value for the column.
The update statement does not and cannot delete rows from a table.
Q: What happens to the records in tbl1 that do not have a match in tbl2?
A: They will not be updated since they have no match from tbl2. From the definition, the INNER JOIN keyword return rows when there is at least one match in both tables. The only keyword that could delete record from your table is the DELETE DML.