substr、substring、substring_index 的区别
时间: 2024-06-11 22:08:18 浏览: 217
substr、substring、substring_index 是三个不同的字符串函数。
1. substr 函数:从一个字符串中返回指定长度的子字符串,它的语法为:
```
substr(str, start, length)
```
其中,str 是要截取的字符串,start 是开始截取的位置(从 1 开始),length 是截取的长度。
例如,substr("hello world", 1, 5) 将返回 "hello"。
2. substring 函数:从一个字符串中返回指定位置的子字符串,它的语法为:
```
substring(str, start, end)
```
其中,str 是要截取的字符串,start 是开始截取的位置(从 1 开始),end 是结束截取的位置(包含在内)。
例如,substring("hello world", 7, 11) 将返回 "world"。
3. substring_index 函数:从一个字符串中返回指定分隔符的子字符串,它的语法为:
```
substring_index(str, delim, count)
```
其中,str 是要截取的字符串,delim 是分隔符,count 是指定返回第几个分隔符后面的子字符串(可以为负数,表示从后往前数)。
例如,substring_index("www.baidu.com", ".", 2) 将返回 "baidu"。
相关问题
substr、substring、substring_index的区别
substr、substring、substring_index都是用于截取字符串的函数,但是它们的具体用法和功能有所不同。
1. substr函数:用于截取字符串中指定位置开始的指定长度的子串。语法为:substr(string,start,length),其中string为要截取的字符串,start为开始位置,length为要截取的长度。例如:substr("hello world", 3, 5)将返回"lo wo"。
2. substring函数:用于截取字符串中指定位置开始到指定位置结束的子串。语法为:substring(string,start,end),其中string为要截取的字符串,start为开始位置,end为结束位置。例如:substring("hello world", 3, 8)将返回"lo wor"。
3. substring_index函数:用于截取字符串中指定分隔符出现的位置之前或之后的子串。语法为:substring_index(string,delimiter,count),其中string为要截取的字符串,delimiter为分隔符,count为要截取的子串的数量,可以为正数表示取分隔符之前的子串,也可以为负数表示取分隔符之后的子串。例如:substring_index("hello.world", ".", 1)将返回"hello",substring_index("hello.world", ".", -1)将返回"world"。
优化下列sql: INSERT INTO borrow_order_2017(id, order_no, borrow_id, glc, libcode, rdid, create_time, should_revert_time, revert_time, status) SELECT * from borrow_order where IF((SUBSTR(borrow_id,1,1) REGEXP '[^0-9.]') = 1,substring(borrow_id,2,4),substring(borrow_id,1,4)) = 2017
可以尝试使用以下优化方法:
1. 使用批量插入语法,将多个INSERT语句合并成一个,可以减少网络通信的次数和开销,提高效率。
2. 在SELECT语句中使用具体的列名代替“SELECT *”,可以避免不必要的列的查询和传输。
3. 在WHERE语句中使用函数索引或者建立虚拟列,可以加快查询速度。
优化后的SQL如下所示:
```
INSERT INTO borrow_order_2017(id, order_no, borrow_id, glc, libcode, rdid, create_time, should_revert_time, revert_time, status)
SELECT id, order_no, borrow_id, glc, libcode, rdid, create_time, should_revert_time, revert_time, status
FROM borrow_order
WHERE SUBSTR(borrow_id,1,1) REGEXP '[^0-9.]' = 1 AND SUBSTR(borrow_id,2,4) = '2017'
OR SUBSTR(borrow_id,1,4) = '2017';
```
这里使用了具体的列名,将IF函数替换为OR语句,并将正则表达式替换为字符串匹配,可以加快查询速度。同时,可以考虑为borrow_id列建立函数索引或者虚拟列,以进一步提高查询速度。
阅读全文
相关推荐
















