今天在整理以前的sql语句的时候,突然发现这样一段:
CASE
WHEN length(a.title)>12 THEN concat(substr(a.title,1,
36),
'…' )
ELSE title
END title
条件查询已经与大家分享过来,这里就来看看concat、substr和length三个函数吧。
concat()函数的作用是将两个字符串合并起来,例如:
表table1
id
1
2
select
concat('你好',id) as b
from table1
结果如下:
id
你好1
你好2
但是orcale也可以用“||”字符来表示链接,
select
'你好'||id as b
from table1 效果是一样的。
以上种方法都可以重复合并。
substr()函数是将字符串拆分开来,与mid()函数效果一样。语法为SUBSTRING ( 字段名, 起始位置, 截取长度),这里强调的是起始位置是从1开始,例如:
表table1
text
12345
234567890123
select
substr(text,3,7) as text
from table1
结果为:
text
345
456789
length()函数是返回字符串的长度,与len()函数效果一样。但是MySQL与orcale有一些区别,例如字段text为”类型“:
select length(text)as length from table1
orcale 得到结果为2,MySQL得到结果为6.