Home  >  Article  >  Database  >  sql截取字段最后以特定字符隔开的内容语句

sql截取字段最后以特定字符隔开的内容语句

WBOY
WBOYOriginal
2016-06-07 17:51:451336browse

文章介绍了一个Mysql批量截取最后隔开的部分到另一字段的SQL语句 有需要的朋友可以参考一下。

字段images保存的数据是: a.jpg;b.jpg;c.jpg

要把images的最后一张图片c.jpg赋值到images_default

执行的SQL语句是

 代码如下 复制代码
UPDATE product SET `image_default` = REVERSE(MID(REVERSE(`images`),1,LOCATE(';',REVERSE(images))-1)) WHERE product_id > 1

下面我们利用了几个函数,参考

REVERSE

REVERSE(str)
    返回颠倒字符顺序的字符串str。

 代码如下 复制代码
    > REVERSE('abc');
            -> 'cba'

    该函数对多字节可靠的。

MID

MID() 函数

MID 函数用于从文本字段中提取字符。
SQL MID() 语法

 代码如下 复制代码
SELECT MID(column_name,start[,length]) FROM table_name

LOCATE

LOCATE(substr,str) , LOCATE(substr,str,pos)
第一个语法返回字符串 str中子字符串substr的第一个出现位置。第二个语法返回字符串 str中子字符串substr的第一个出现位置, 起始位置在pos。如若substr 不在str中,则返回值为0

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn