0
点赞
收藏
分享

微信扫一扫

MYSQL 寒假自学 2022 十二 (三)

窗外路过了谁 2022-02-15 阅读 74
-- 字符串函数

-- 1,获取字符串字符个数
select char_length('hello '); #6空格也算
select char_length('你好吗'); #3 汉字算一个
-- charactr_length 和char_length 的功能相同
-- length 取长度,返回的单位是字节
select length('hello '); #6
select length('你好吗'); #9 UTF-8编码中,汉字占三个字节

-- 2,字符串合并
select concat('hello','world','scx');
-- 指定分隔符进行字符串合并
select concat_ws('-','hello','world');

-- 3,返回字符串在列表中第一次出现的位置
select field('aaa','aaa','bbb','ccc'); #1
select field('bbb','aaa','bbb','ccc'); #2
select field('ddd','aaa','bbb','ccc'); #0,没查询到
 
--  4,去除字符串左边的空格
select ltrim('  abcd         ');
-- 去除右端的空格
select rtrim('  abcd         ');
-- 两端空格都去掉,中间的字符串无法去除
select trim('  abcd         ');

-- 5,字符串截取
select mid('helloworld',2,3);#从第二个位置截取三个字符,长度为3

-- 6,获取字符串a在字符串b中出现的位置,先看到哪个返回哪个
select position('abc' in 'helloabcworld');#6
select position('abc' in 'habcelloabcworld');#2

-- 7,字符串替换,如果有两个aaa就都替换掉
select replace('helloaaaworld','aaa','bbb');#hellobbbworld
select replace('aaahelloaaaworld','aaa','bbb');#bbbhellobbbworld
-- 8,字符串反转
select reverse('hello');#olleh
举报

相关推荐

0 条评论