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)
Related
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;
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"
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.
I am trying to Update a column in a table in one database after doing two inner joins, one of which is with a table from another database on the same server. I was trying to follow this solution here:
Update Query with INNER JOIN between tables in 2 different databases on 1 server
This isn't working for me. It gives me: you have an error in your syntax near s_u
Here's my attempt (UPDATE: I removed the code sanitation. So this is the exact code I'm running now):
UPDATE s_u
SET s_u.bill_address_id=spree_billing.id
FROM spree_users AS s_u
INNER JOIN magento.customer_entity_int AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN spree_addresses AS spree_billing
ON spree_billing.magento_address_id=default_billing.value;
You reference to something called spree_billing which does not appear anywhere in your statement. You need to fix it. The problem is probably here:
UPDATE s_u
SET s_u.bill_address_id=billing.id -- modify to billing, as it is what third_table's alias is called
FROM some_table AS s_u
INNER JOIN magento.another_table AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN third_table AS billing
ON billing.magento_address_id=default_billing.value;
please can you try this :
UPDATE spree_users AS s_u
INNER JOIN magento.customer_entity_int AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN spree_addresses AS spree_billing
ON spree_billing.magento_address_id=default_billing.value
SET s_u.bill_address_id=spree_billing.id;
I didn't tried your tables but test on my sample test
The answer in you answer is for SQL-Server. you should look this link. UPDATE multiple tables in MySQL using LEFT JOIN
updated
so, syntax error is removed but it takes long. I want to check how many records matches JOIN or join is performing efficiently. please can you post following query's output?
SELECT COUNT(*)
FROM spree_users AS s_u
INNER JOIN magento.customer_entity_int AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN spree_addresses AS spree_billing
ON spree_billing.magento_address_id=default_billing.value
I tried looking for a question such as this on SO but a lot of them were outer joins with more complicated select clauses whereas my question is more simple and should be a faster reference for newbies at MySQL.
Being used to Oracle SQL, I was trying to join three tables and perform a delete like so:
DELETE * FROM tbl_login, tbl_profile, images
WHERE tbl_login.loginid = tbl_profile.loginid
AND tbl_profile.loginid = images.loginid
AND loginid = 'derek';
In MySQL my attempt is:
DELETE FROM tbl_profile, images, tbl_login
USING tbl_profile INNER JOIN images INNER JOIN tbl_login
WHERE tbl_profile.loginid = images.loginid
AND images.loginid = tbl_login.loginid
AND loginid='derek';
I ran this in the SQL section of PHPMyadmin and it told me that loginid was ambiguous which I thought was funny because if I'm joining the three tables why would it be ambiguous? So I edited it and made it
tbl_login.loginid = 'derek'
that deleted the correct row from the tbl_login table but it ended up deleting all the rows from my other tables. What am I doing wrong here?
Remove tbl_profile and images from your FROM clause.
I think your query should look something like this (note the different way the join conditions are defined):
DELETE FROM tbl_profile, images, tbl_login
USING tbl_profile INNER JOIN images ON images.loginid = tbl_profile.loginid
INNER JOIN tbl_login ON tbl_login.loginid = tbl_profile.loginid
WHERE tbl_login.loginid='derek';
I assume you want to delete the rows from all 3 tables. If you only want to delete from tbl_login, the previous answer tells you how to do it :)