请帮助我解决以下 mysql 查询, 我有一个主表,如下所示:
id phone phone2
======================================
1 98998 | 58877
2 98998 | 58877
3 98998
我想通过电话列更新phone2并尝试使用以下子查询,但我没有得到所需的结果,如果可能的话请帮助获得结果:
update main a
set a.phone2 = (SELECT substring_index(b.phone,'|',-1)
FROM main b
where b.phone like '%|%' and where a.id=b.id)
最佳答案
试试这个:
UPDATE main a
INNER JOIN (SELECT id, substring_index(phone,'|',-1) AS phone
FROM main
WHERE phone LIKE '%|%'
) b
ON a.id = b.id
SET a.phone2 = b.phone;
关于mysql - 如何使用子查询更新特定列多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11827145/