MySql update with subselect

让人想犯罪 __ 提交于 2019-12-12 05:15:53

问题


I have a mysql database table where there is an email column which can contain up to two emails separated by an semicolon. What i want to achieve is write the second email address in the email field into another column email2 This is what I tried so far without success:

UPDATE user AS u
SET email2 = (SELECT SUBSTRING_INDEX(email, ';', -1)
              FROM user
              WHERE user.id=u.id)
WHERE username LIKE "%;%"

When searching for this problems there were some solutions unsing a second subquery within the subquery but none of them really matched my problem.

If anybody has a solution please post it. I have been stuck for at leas 2 hours.

Thank you very much.


回答1:


No need to complicate your life :

UPDATE user 
SET email2 = SUBSTRING_INDEX(email, ';', -1) 
WHERE username LIKE '%;%';

and to clean first column in same query

UPDATE user 
SET 
  email2 = SUBSTRING_INDEX(email, ';', -1),
  email = SUBSTRING_INDEX(email, ';', 1),
WHERE username LIKE '%;%';


来源:https://stackoverflow.com/questions/14565176/mysql-update-with-subselect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!