mysql8学习笔记⑧数据库的字符串函数

mysql8学习笔记⑧数据库的字符串函数

-- 出于SEO优化的目的,我们需要合并显示课程分类名称和课程标题

select concat(b.type_name,a.title) as title_type_name
from imc_course a
join imc_type b on a.type_id = b.type_id

-- 出于SEO优化的目的,我们需要合并显示课程分类名称和课程标题,用||相连

select concat_ws('||',b.type_name,a.title) as title_type_name
from imc_course a
join imc_type b on a.type_id = b.type_id

select left('www.imooc.com',3)

select substring('www.imooc.com', 5)

截取IP地址的后两段

select substring_index('192.168.0.100','.',-2)

截取课程表中标题 - 前的内容

select title
            ,locate('-',title)
            ,substring(title,1,locate('-',title)-1) 
from imc_course;

select title
            ,locate('-',title)
            ,substring(title,1,locate('-',title)-1)
            ,substring_index(title,'-',1)
from imc_course;

使用trim删除字符串左右两边空格和指定字符

 

使用case when 对结果进行描述

select user_nick
                ,case when sex = 1 then ''
                    when sex = 0 then ''
                    else '未知'
                end as  '性别'
                
from imc_user;

过滤出男性会员
select user_nick
                ,case when sex = 1 then ''
                    when sex = 0 then ''
                    else '未知'
                end = ''
                
from imc_user;

原文地址:https://www.cnblogs.com/reblue520/p/13523849.html