I am trying to make update statement in MySql table - just migrating data from one user to second user. The concrete row should be migrated only when the row with same unique ID devid doesn't exist yet for that second user.
The update statement that I used is:
update userdata as userdata1 set userid=${newuser}
where
userid=${olduser} and
userdata1.devid !=
(select devid from userdata as userdata2
where
userdata2.userid = ${newuser} and userdata2.`devid` = userdata1.`devid`)
The MySql returns:
You can't specify target table 'userdata' for update in FROM clause
But from the other way, similar select statement works, or similar update statement, when I test data on another that same table works as well. So that is this some limitation, that there is not possible to do select on same table during update? And how to manage this?
Thanks,
Jindrich
You can use a left join:
UPDATE userdata u1
LEFT JOIN userdata u2 ON u2.userid = :newuser AND u2.devid = u1.devid
SET u1.userid = :newuser
WHERE u1.userid = :olduser
AND u2.userid IS NULL;
Related
I need to update the table 1 column with the table 2 column value. So I try doing this in mySQL
UPDATE location_role_user
SET location_role_user.TENANT_CODE = usr.TENANT_CODE
FROM
users usr
JOIN
users ON location_role_user.LOGIN_ID = usr.LOGIN_ID
You seem to be using SQL Server update join syntax. The MySQL version looks like this:
UPDATE location_role_user lru
INNER JOIN users usr
ON lru.LOGIN_ID = usr.LOGIN_I
SET
lru.TENANT_CODE = usr.TENANT_CODE;
This can be done with ease using the following query.
UPDATE location_role_user loc INNER JOIN users usr
ON loc.LOGIN_ID = usr.LOGIN_ID
SET loc.TENANT_CODE = usr.TENANT_CODE;
Use a subquery:
UPDATE location_role_user
SET location_role_user.TENANT_CODE=
(SELECT usr.TENANT_CODE FROM usr WHERE location_role_user.LOGIN_ID = usr.LOGIN_ID);
I'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;
I need to update 1 table with data from another where two fields match. I have a query but it's just locking up.
I have an employees_training_courses table
I have a company_training_categories table
I need to get the ID from company_training_categories where both the name and the account_id are the same in both tables.
So far I have this...
update employee_training_courses tc join company_training_categories ctc on ctc.name = tc.name AND ctc.account_id = tc.account_id set tc.company_training_category_id = ctc.id;
i can leave the query running, but it's clearly getting hung up somewhere !
This is your query:
update employee_training_courses tc join
company_training_categories ctc
on ctc.name = tc.name AND ctc.account_id = tc.account_id
set tc.company_training_category_id = ctc.id;
This is a very reasonable query. You probably just need indexes to speed it up. I would recommend:
create index idx_company_training_categories_2 on company_training_categories(name, account_id)
or even:
create index idx_company_training_categories_3 on company_training_categories(name, account_id, id)
I want to update a field in table1 with another field in table2.I wrote the following query but it is not working.
UPDATE tempdata A
SET A.new_user_id =
(SELECT B.id FROM user B
WHERE A.usr_email = B.usr_email)
It is giving "#1242 - Subquery returns more than 1 row" error. Anybody please help me.
UPDATE tempdata A, user B
SET A.new_user_id = B.id
WHERE A.usr_email = B.usr_email
you can still join tables even if it is an update statement.
UPDATE tempdata A
INNER JOIN user B
ON A.usr_email = B.usr_email
SET A.new_user_id = B.id
Beware that error means that in table user there are more than 1 rows with field usr_email equals to tempdata's one. Check for dupes before runniing the actual update statement with the LIMIT 1 a suggested by Salil
This is also one way to handle this scenerio
UPDATE A
SET A.new_user_id = B.id
FROM tempdata A
INNER JOIN user B ON A.usr_email = B.usr_email
I am trying to update all transaction_subcategory_id's in transaction to match_subcategory_id in match where match_name in match is the same as transaction_subcategory_name in transaction.
Its got to be simple, just not getting very far with it...
this is our latest attempt...
UPDATE transaction JOIN match ON match.match_name = transaction.transaction_name
SET transaction.transaction_subcategory_id = match.match_subcategory_id;
tables
match
--------------------------
match_id
match_name
match_subcategory_id
transaction
--------------------------
transaction_id
transaction_name
transaction_subcategory_id
UPDATE `transaction` SET `transaction`.`transaction_subcategory_id` = `match`.`match_subcategory_id`
JOIN `match` ON `match`.`match_name` = `transaction`.transaction_name;
Just switch the position of the SET and the JOIN and it should work.
MySQL Docs on UPDATE statement
The docs only show the implicit join:
UPDATE `transaction`, `match` SET `transaction`.`transaction_subcategory_id` = `match`.`match_subcategory_id`
WHERE `match`.`match_name` = `transaction`.`transaction_name`;
But they say you can use any JOIN syntax that works on SELECT on UPDATE, so your query should work when switching SET and JOIN.
try with SET before JOIN on your query