Updating a column from another column conditioned on related table - mysql

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"

Related

MySQL Update statement after adding extra condition at WHERE

I have this MySQL Update statement. It works fine.
UPDATE Table1
SET Table1_field1='field1_content', Table1_field2='field2_content'
where Table1_field3=2
All the fields above belong to the same table. I then added an extra condition AND Table2.fieldname='XXX' to the WHERE clause
UPDATE Table1
SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
where Table1_fieldname3=2 AND Table2.fieldname='XXX'
This time, the SQL statement fails. The error is "unknown column Table2.fieldname in where clause". However, Table2.fieldname does exist.
In order to be able to use fields from Table2 in your query you'll need use a JOIN between Table1 and Table2.
A JOIN effectively combines a row from each table into a single row for your query, based on a provided condition.
For example if both Table1 and Table2 have a column tableID, we can combine rows from each table where the tableIDs match.
The query would then look like below:
UPDATE Table1
JOIN Table2
ON Table1.tableID = Table2.tableID
SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
WHERE Table1_fieldname3=2 AND Table2.fieldname='XXX';
The JOIN keyword is equivalent to INNER JOIN. There are different types of JOINs available and I'd recommend reading up about them.
Here's a reference image to give you an idea of the different types:
you need to join table 1 and table2; then you can update
UPDATE Table1 AS b
INNER JOIN Table2 AS g ON b.id = g.id SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
where Table1_fieldname3=2 AND g.fieldname='XXX'

Update a table with JOIN - MySQL

I am trying to update a column with a calculated column in a inner join.
The logic is simple but I am struggling with the syntax
(this is just a dummy SQL, to explain what I am trying to accomplish - it does not run)
UPDATE t1
SET t1.BodyText = t2.final
from Questions as t1
INNER JOIN translations as t2
on t2.QuestionId=t1.QuestionID
CONCAT(t1.BodyText,t2.QuestionBodyText) as final
The task is simple, concat a question with its translation. I found some questions related to this issue on stackoverflow, but they where no help, maybe because they were discussing SQL Server.
Similar:
Update a table using JOIN in SQL Server?
I tried that:
UPDATE Questions t1
JOIN translations t2
on t1.QuestionID=t2.QuestionId
SET t1.BodyText = CONCAT(t1.BodyText,t2.QuestionBodyText)
But it does not have any effect on the database.
This is an equivalent SELECT that works:
SELECT CONCAT(t1.BodyText,t2.QuestionBodyText) FROM Questions t1
JOIN translations t2
on t1.QuestionID=t2.QuestionId
Update, when I used this update query on phpmyadmin it worked, on workbench it did not..
Ah, your syntax is out of whack.
Update Questions t1
join translations t2
on t2.QuestionID = t1.QuestionID
set t1.BodyText = concat(t1.bodytext,t2.questionbodytext)

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.

Do you have to join tables "ON" fields or can you just equate them in there where clause?

We have been doing queries a bunch of different ways and queries have been working when we do a
SELECT t.thing FROM table1 t JOIN table2 s WHERE t.something = s.somethingelse AND t.something = 1
and it worked with all queries except one. This one query was hanging forever and crashes our server, but it apparently works if we do it like:
SELECT t.thing FROM table1 t JOIN table2 s ON t.something = s.somethingelse WHERE t.something = 1
We are trying to figure out if the problem is due to the query structure or due to some corruption in the account we are trying to query.
Is the first syntax correct? Thanks.
You need to use the ON clause. Though you can also join with commas, e.g.: SELECT * FROM table1, table2;
Hope that helps!
There are different ANSI formats.. you can use
Select ...
from tbl1 join tbl2 on tbl1.fld = tbl2.fld
OR
select ...
from tbl1, tbl2
where tbl1.fld = tbl2.fld...
The explicit join is the more common format where you are explicitly showing developers after yourself how the tables are related without respect to filtering criteria.
Your first syntax miss the ON. When you join it is mandatory to tell ON what fields the join is happening.
I would recommend using JOIN ON over WHERE to do your joins.
1) your where clause will be easier to read since it will not be pollute by join where clause.
2) your join section is easier to read and understand.
We all agree both method works, but the JOIN one is better due to theses points.
my 2 cents

How to update two tables in one statement?

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.