原始表如下:

emp

dept

第1题,6.1遍历字符串

例:把emp表中的ename等于KING的字符串拆开来显示为4行

需要最终结果:

select substr(ename, t10.id,1) as string

from emp, t10

where t10.id <= length(ename)

and ename='KING'

注意length()函数,而不是len()

第2题,6.3统计字符出现的次数

例:统计字符串中有多少个逗号 ‘10,CLARK,MANAGER’

需要最终结果:

答:

select round((length('10,CLARK,MANAGER') - length(replace('10,CLARK,MANAGER',',','')))/length(',')) as cn

第3题,6.4删除不想要的字符

例:从下表中,删除所有的数字0和元音字母,并将删除后的值显示在strippde1列和strippde2列中

需要最终结果:

答:

select ename,

replace(

replace(

replace(

replace(

replace(

ename,'A',''),'E',''),'I',''),'O',''),'U','') strippde1,

sal,

replace(sal,'0','') strippde2

from emp

第4题,6.7提取姓名的首字母

例:希望把姓名变成首字母的形式,如:Stewie Griffin,得到S.G

(姓名有两种形式要做考虑:1.First Name+Last Name 2.First Name+Middle Name+Last Name)

需要最终结果:

答:

select case when cnt=2

then concat_ws('.', substring(substring_index(name,' ',1),1,1),

substring(name, length(substring_index(name,' ',1))+2,1),

substring(substring_index(name,' ',-1),1,1))

else concat_ws('.', substring(substring_index(name,' ',1),1,1),

substring(substring_index(name,' ',-1),1,1))

end as initials

from (select name, length(name)-length(replace(name,' ','')) as cnt

from (select replace('Stewie Griffin','.','') as name) as y

) as x

第5题,6.10创建分隔列表

例:想把行数据变成以某种符号分隔的列表,例如以逗号分隔,而不是常见的竖排的列数据形式

竖排形式数据:

需要最终结果:

答:

select deptno,

group_concat(ename separator ',') as emps

from emp

group by deptno

#如果要考究一点也可以在group_concat()函数中做点花样

select deptno,

group_concat(ename order by empno separator ',') as emps

from emp

group by deptno

第6题,6.11分隔数据转换为多值in列表

例:如下in列表中只有一个字符串,希望字符换能被当作以逗号分隔的数值列表

select * from emp

where empno in ('7654,7698,7782,7788')

需要最终结果:

答:

select empno, ename, sal, deptno

from emp

where empno in (select substring_index(substring_index(list.vals,',',t10.id),',',-1) as empno

from t10, (select '7654,7698,7782,7788' as vals) list

where t10.id <= (length(list.vals)-length(replace(list.vals,',','')))+1

)

第7题,6.12按字母表顺序排列字符

例:按照字母表顺序,对姓名ename进行单个字母排序

需要最终结果:

答:

select ename, group_concat(c order by c separator '') new_name

from (select ename, substr(ename, t10.id,1) c

from emp, t10

where t10.id <= length(ename)

) x

group by ename

第8题,6.14提取第n个分隔子字符串

例:希望提取下列每一行中,第二个名字

需要最终结果:

答:

#对应的视图,做出了原始表

create view v as

select 'mo,larry,curly' as name

union all

select 'tina,gina,jaun,regina,leena'

#方法1:提取第二个名字

select substring_index(substring_index(v.name,',',2),',',-1) name

from v

#方法2:比较通用id可根据需求变动

select name

from (select t10.id, substring_index(substring_index(v.name,',',t10.id),',',-1) name

from v, t10

where t10.id <= length(v.name)-length(replace(v.name,',',''))+1

) x

where id=2

(易错:最后一句where id=2,不能写成where t10.id=2,否则会报错Error Code: 1054)

mysql 处理字符串 减断,MySQL字符串处理[8题]相关推荐

  1. mysql 处理字符串 减断_Mysql处理字符串函数(转)

    标签:http://www.jb51.net/article/27458.htm 感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用 ...

  2. Mysql:mysql 控制台程序的提示符 prompt 字符串设置

    The prompt command reconfigures the default mysql> prompt. The string for defining the prompt can ...

  3. mysql strcmp s1 s2_MySQL函数基础——字符串函数详解

    昨天,咱们对MySQL的数学函数进行了讲解,今天,咱们再来解析MySQL字符串函数. 字符串函数主要用来处理数据库中的字符串数据,MySQL中字符串函数有:计算字符串长度函数.字符串合并函数.字符串替 ...

  4. mysql 查找字符位置_MySQL数据库中如何查看一个字符串在另一个字符串中第一次出现的位置呢?...

    摘要: 下文讲述MySQL数据库中查看一个字符串第一次出现的位置的方法分享,如下所示: 实现思路: 方式1: 使用系统函数LOCATE(substr,str)即可获取 substr字符串在str中第一 ...

  5. Mysql数据库函数(数字,字符串,日期时间)

    文章目录 Mysql数据库函数(数字,字符串,日期时间) 数学函数 字符串函数 日期函数 Mysql数据库函数(数字,字符串,日期时间) 数学函数 abs(x) :返回x的绝对值 rand() :返回 ...

  6. mysql sql字符串连接函数_Mysql字符串连接函数 CONCAT()与 CONCAT_WS()

    从数据库里取N个字段,然后组合到一起用","分割显示,起初想到用CONCAT()来处理,好是麻烦,没想到在手册里居然有提到 CONCAT_WS(),非常好用. CONCAT_WS( ...

  7. php mysql 随机字符串_MySQL_Mysql 自定义随机字符串的实现方法,前几天在开发一个系统,需要 - phpStudy...

    Mysql 自定义随机字符串的实现方法 前几天在开发一个系统,需要用到随机字符串,但是mysql的库函数有没有直接提供,就简单的利用现有的函数东拼西凑出随机字符串来.下面简单的说下实现当时. 1.简单 ...

  8. mysql更新字符串中某个字符串_mysql更新某个字符串字段的部分内容

    如果现在需要Mysql更新字段重部分数据,而不是全部数据,应该采用何种方法呢?下面介绍了两种情况下Mysql更新字段中部分数据的方法,供您参考. Mysql更新字段中部分数据第一种情况: update ...

  9. mysql 字符串 截取字母_MySQL字符串函数:字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

最新文章

  1. k8s 下线node正确处理姿势
  2. Index of sql server
  3. Java多线程 ——线程基础和锁锁锁
  4. WCF技术剖析之六:为什么在基于ASP.NET应用寄宿(Hosting)下配置的BaseAddress无效...
  5. C++ 工具类 —— 词条类(Entry)
  6. scratch少儿编程航天主题:模拟航天飞机飞行
  7. linux超级块编辑,在EXT4 linux系统上模拟丢失的超级块错误
  8. C语言alloc函数总结
  9. android模拟器 diy,史莱姆机DIY模拟器
  10. 一种无法用言语表达的爱——父爱
  11. mac系统按Esc键无法切换vim编辑模式
  12. Flink 网络流控与反压机制
  13. #内存泄露# #valgrind# valgrind使用
  14. lesson 21 mad or not 是不是疯了-把什么逼疯,be driving sb mad,live near 住在什么附近,过去将来时的被动式 will be done
  15. arcgis新建图层信息复制_怎么在arcgis中把一个图层复制到另一个图层上
  16. 如何清理C盘的垃圾文件
  17. 《大话设计模式》总结 (更新ing)
  18. x98air2+android+升级,[x98 air 3g平板]安装任意版本32位win10的方法
  19. C++实现:求坐标系中的某一点到原点的距离(使用构造函数)
  20. 如何在pc上安装安卓应用程序

热门文章

  1. 大小写转化php,怎样用PHP做出人名币大小写的方法转换
  2. js图片上传功能前端
  3. Cadence Virtuoso IC617从原理图建立器件和生成版图
  4. 游戏背景音乐的两个特殊类型
  5. 达梦数据库配置SSL认证加密
  6. JAVA:如何读写txt文件,并解决中文乱码问题
  7. Python爬取豆瓣电影评论数据(通用模板代码)----以《中国医生》为例
  8. html语言怎么做到走马灯,HTML+CSS入门 如何实现跑马灯/走马灯效果
  9. QNX独特的工程目录结构
  10. 网络安全攻防演练项目项目流程