Update the table 1 column with the values of table 2 colum - mysql

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);

Related

INNER JOIN ON multiple attributes not working

I need to update a column with values from another table ... but it takes forever or aborts. A specific security (isin) can be listed at multiple exchanges (mic) ... so I think I need to have two conditions in the INNER JOIN ... ON (??). Are my attempts below correct? I have about 170,000 records in the table with 40,000 unique isins.
First try:
SQL:
SET SESSION SQL_BIG_SELECTS = 1;
UPDATE securities_live t1
INNER JOIN securities_prev t2
ON t1.isin = t2.isin AND t1.mic = t2.mic
SET t1.prev_close = t2.close;
Second try:
SQL:
SET SESSION SQL_BIG_SELECTS = 1;
UPDATE securities_live t1
INNER JOIN securities_prev t2
ON (t1.isin = t2.isin AND t1.mic = t2.mic)
SET t1.prev_close = t2.close;
Edit regarding indexes for both tables at the moment:
Indexes (securities_live):
Primary|Unique=Yes|Packed=no|Column=id|Cardinality=166776|Collation=A
Indexes (securities_prev):
Primary|Unique=Yes|Packed=no|Column=id|Cardinality=166776|Collation=A
In both tables I have a primary key on column 'id'. So e.g. in table securities_live 'Create a new index' one for column isin and another one for column mic? What about Index name and index type (Primary, Index, Unique, Fulltext)? Size?
For this query:
UPDATE securities_live t1 INNER JOIN
securities_prev t2
ON t1.isin = t2.isin AND t1.mic = t2.mic
SET t1.prev_close = t2.close;
I would suggest an index on securities_prev(isin, mic, close).
However, I suspect that you are updating all or almost all records. If that is the case, it is usually faster to truncate the table and re-populate it with insert. Update is best used for updating a relatively small number of rows.
As the commenters have noted, since you have your indexes set, I suggest you try to do these piecemeal. Do them at 5k a time until it is complete. Try 10k. Obviously you cannot do 170k or 40k. I have had many times where a database had millions of rows, and I had to to them 100k or less at a time. This was because of the limitations of the hardware.
For example,
UPDATE top(1000) securities_live t1
INNER JOIN securities_prev t2
ON t1.isin = t2.isin AND t1.mic = t2.mic
SET t1.prev_close = t2.close;
You may wish to use Order By so you know what records are what and you need keep track of what has been updated.
See here,
how can I Update top 100 records in sql server
Sorry, I just read you were using MySQL,
MySQL - UPDATE query with LIMIT
Try This:
SET SESSION SQL_BIG_SELECTS = 1;
UPDATE securities_live t1
INNER JOIN securities_prev t2
ON (t1.isin = t2.isin AND t1.mic = t2.mic)
SET t1.prev_close = t2.close;

MySQL Conditional Update in same table

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;

update field with another field another table

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

MySql Join Query Problem

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

Update MySql table - test uniqueness of one field during update

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;