这是我的桌子:
-- mytb
+----+---------------------------+
| id | content |
+----+---------------------------+
| 1 | <br> whatever .. |
| 2 | whatever <br> whatever .. |
| 3 | <br> whatever .. |
| 4 | <br> whatever <br> .. |
+----+---------------------------+
我需要删除字符串开头的所有<br>
。我可以在PHP正则表达式中使用^
来指定它。如何在MySQL中做同样的事情?
update mytb set content = trim(content)
它只是删除周围的空格。
答案 0 :(得分:0)
SELECT REPLACE('content','<br>','') FROM MYTB
WHERE content LIKE '<br>%'
答案 1 :(得分:0)
您可以在MYSQL中使用REGEXP:
SELECT REPLACE('content','<br>','') as content FROM table WHERE content REGEXP '^<br>'
答案 2 :(得分:0)
您可以使用如下查询:
SELECT REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2');
样品
MariaDB [(none)]> SELECT REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2');
+------------------------------------------------------------------------+
| REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2') |
+------------------------------------------------------------------------+
| Hello<br>world |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT REGEXP_REPLACE('Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2');
+--------------------------------------------------------------------+
| REGEXP_REPLACE('Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\\2') |
+--------------------------------------------------------------------+
| Hello<br>world |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT REGEXP_REPLACE('<br>Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2');
+--------------------------------------------------------------------+
| REGEXP_REPLACE('<br>Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2') |
+--------------------------------------------------------------------+
| Hello<br>world |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT REGEXP_REPLACE('Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2');
+----------------------------------------------------------------+
| REGEXP_REPLACE('Hello<br>world','^(<br>)*(.*?)(<br>)*$','\\2') |
+----------------------------------------------------------------+
| Hello<br>world |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
答案 3 :(得分:-1)
SELECT CASE WHEN substr(content,1,4) ='<br>'
THEN substr(content,5,len(content))
ELSE content
END as content
答案 4 :(得分:-1)
使用带有if条件的MYSQL子字符串。
SET content = TRIM
(IF
(SUBSTRING
(TRIM(content), 1, 4) = '<br>',
SUBSTRING(TRIM(content), 5), content
)
);
答案 5 :(得分:-2)
MYSQL具有可以使用的substring
函数:
UPDATE mytb SET content = SUBSTRING(content, 5)
WHERE content LIKE '<br>%'