MySQL表中有数千个字符串,如下所示
some-text-for-read/id123456
some-other-text-to-read-too/id456789我想让他们
/id123456
/id456789如何使用通配符作为文本部分?我尝试了所有我发现的“替换%id%”之类的查询,但是没有起作用,因为我可能做得不对。
发布于 2015-01-29 01:53:44
试试这个:
UPDATE your_table set your_field = CONCAT('/', SUBSTRING_INDEX(your_field, '/', -1)) 发布于 2015-01-29 01:41:57
使用MySQL字符串函数 LOCATE()和SUBSTR()的组合,您可以使用LOCATE()查找第一个/字符的字符串中的位置,使用SUBSTR()返回以原始字符串中该位置开头的子字符串。
您可以将其全部放入UPDATE语句中:
UPDATE your_table
SET your_column = SUBSTR(your_column, LOCATE('/', your_column))
-- Only on rows where the / is present, if there are non-matching rows
-- LOCATE() returns 0 when the string isn't found
WHERE LOCATE('/', your_column) > 0示例:
> SELECT SUBSTR('some-other-text-to-read-too/id456789', LOCATE('/', 'some-other-text-to-read-too/id456789')) AS val;
+-----------+
| val |
+-----------+
| /id456789 |
+-----------+您需要WHERE子句的原因--如果该行中没有/,那么所有内容都将被删除:
/* This has no '/' */
> SELECT SUBSTR('some-other-text-to-read-tooid456789', LOCATE('/', 'some-other-text-to-read-tooid456789')) AS val;
+-----+
| val |
+-----+
| |
+-----+https://stackoverflow.com/questions/28205608
复制相似问题