D
D
d1zz742021-02-10 11:14:40
MySQL
d1zz74, 2021-02-10 11:14:40

How to change a string in MySql depending on the data?

Probably not correctly asked a question in heading, but, karoch
There are two tables (example):
Table1 where users.
Table2 where well, like:
user1 | user2
id1 | id123
id2 | id123
id3 | id123
And I have a list like this (example):
user1 | user2
id1 | id5
id2 | id6
id3 | id7

Ie in table2 it is necessary to change the value of user2 depending on the user's id according to the list above.
Where user1 id1 - user2 should be id5, where id2 - id6 and so on

upd:
Well, I'll try to explain it differently:
SET @user1 = 1
,
@user2 = 5
;

UPDATE table2 set user2 = @user2
WHERE user2 = 123 and user1 = @user1 Do

this several times, but only change the values ​​of @user1 and @user2 from here:
user1 | user2
id1 | id5
id2 | id6
id3 | id7

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-02-10
@d1zz74

Here is a possible option:

update users_match
set usr2 = case
  when usr1 = 1 then 5
  when usr1 = 2 then 6
  when usr1 = 3 then 7
  else usr2
end;

MySQL fiddle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question