测试表
mysql> desc t1;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(6) | YES | | NULL | |
| addr | varchar(30) | YES | | NULL | |
| jixiao | decimal(6,2) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
mysql> select * from t1;
+------+-----------+-----------+---------+
| id | name | addr | jixiao |
+------+-----------+-----------+---------+
| 1 | 三毛 | beijing | 550.00 |
| 2 | liu | shanghai | 300.00 |
| 3 | NULL | guangxi | -210.55 |
| 4 | meng | nanjing | NULL |
| 5 | wang | NULL | -532.30 |
| 6 | 赵晓燕 | beijing | 1800.00 |
| 7 | tao | beijing | 3200.20 |
| 8 | bao | 河北省 | 3000.00 |
| 9 | jing | hangzhou | 778.00 |
| 10 | 董磊 | beijing | 880.37 |
| 11 | tian | tianjin | -354.00 |
| 12 | NULL | hennan | -18.30 |
| 13 | ali | 上海市 | 5432.10 |
| 14 | lei | beijing | 5.50 |
+------+-----------+-----------+---------+
用来获取某列某行的长度
统计的是id=1 这行name 所对应值有几个字符
mysql> select id,addr,length(addr) from t1 where id>10;
+------+-----------+--------------+
| id | addr | length(addr) |
+------+-----------+--------------+
| 11 | tianjin | 7 |
| 12 | hennan | 6 |
| 13 | 上海市 | 9 |
| 14 | beijing | 7 |
+------+-----------+--------------+
上海市是三个字符,但显示出来是9。如果直接使用length 来统计汉字的话是不准确的,要
想统计汉字就要使用char_length();
如果是length 还是char_length,统计的结果都是字符数,和字节没关系。
mysql> select id,addr,char_length(addr) from t1 where id>10;
mysql> select id,addr,char_length(addr) from t1 where id>10;
+------+-----------+-------------------+
| id | addr | char_length(addr) |
+------+-----------+-------------------+
| 11 | tianjin | 7 |
| 12 | hennan | 6 |
| 13 | 上海市 | 3 |
| 14 | beijing | 7 |
+------+-----------+-------------------+
之前的时候我们给大家讲过字符串的类型char()和varchar(),如果能用短的,就不要用
长的,比如char(10)够用就不要使用char(30),如果一开始就选错了,那么这列字符串的长
度我们是可以减小的,但是要注意减小不能小于这列中最长的哪个字符。比如有一行值长度
为9,那么你就不能把字符串长度改成char(8),如果知道哪行长度最长就可以使用length 或
者char_length。
concat 是可以连接多个字符的, concat(‘ss’, ‘yy’, ‘zz’)
格式
concat(列名, ‘字符’)
mysql> select id,name,addr,concat(‘%%‘,name,‘%%‘),
-> concat(‘$‘,addr,‘$‘) from t1 where id<6;
+------+--------+----------+------------------------+----------------------+
| id | name | addr | concat(‘%%‘,name,‘%%‘) | concat(‘$‘,addr,‘$‘) |
+------+--------+----------+------------------------+----------------------+
| 1 | 三毛 | beijing | %%三毛%% | $beijing$ |
| 2 | liu | shanghai | %%liu%% | $shanghai$ |
| 3 | NULL | guangxi | NULL | $guangxi$ |
| 4 | meng | nanjing | %%meng%% | $nanjing$ |
| 5 | wang | NULL | %%wang%% | NULL |
+------+--------+----------+------------------------+----------------------+
可以把几个字符结合在一起,连接起来。
比如姓名是一列,地址是一列,想把这两列结合在一起,就可以使用这个函数。
mysql> select id,concat(‘姓名:‘,name,‘地址:‘,addr) from t1;
+-----+--------------------------------------+
| id | concat(‘姓名:‘,name,‘地址:‘,addr) |
+-----+--------------------------------------+
| 2 | 姓名:liu地址:shanghai |
| 4 | 姓名:meng地址:nanjing |
| 7 | 姓名:tao地址:beijing |
| 9 | 姓名:jing地址:hangzhou |
| 11 | 姓名:tian地址:tianjin |
+------+-------------------------------------------+
concat_ws(‘分隔符’, 列名,字符)
mysql> select id,name,concat_ws(‘:‘,‘用户名‘,name)from t1 where id>6;
+------+--------+---------------------------------+
| id | name | concat_ws(‘:‘,‘用户名‘,name) |
+------+--------+---------------------------------+
| 7 | tao | 用户名:tao |
| 8 | bao | 用户名:bao |
| 9 | jing | 用户名:jing |
| 10 | 董磊 | 用户名:董磊 |
| 11 | tian | 用户名:tian |
| 12 | NULL | 用户名 |
| 13 | ali | 用户名:ali |
| 14 | lei | 用户名:lei |
+------+--------+---------------------------------+
用来做替换的
格式
insert(列名, 起始位, 长度, ‘替换的字符’)
mysql> select id,insert(name,2,3,‘*‘) from t1 where id<6;
+------+----------------------+
| id | insert(name,2,3,‘*‘) |
+------+----------------------+
| 1 | 三* |
| 2 | l* |
| 3 | NULL |
| 4 | m* |
| 5 | w* |
+------+----------------------+
mysql> select id,insert(name,2,1,‘$$$$$‘) from t1 where id<6;
+------+--------------------------+
| id | insert(name,2,1,‘$$$$$‘) |
+------+--------------------------+
| 1 | 三$$$$$ |
| 2 | l$$$$$u |
| 3 | NULL |
| 4 | m$$$$$ng |
| 5 | w$$$$$ng |
+------+--------------------------+
把小写转换成大写:
mysql> select upper(‘mysql‘);
+----------------+
| upper(‘mysql‘) |
+----------------+
| MYSQL |
+----------------+
把小写字母转成大写字母
mysql> select lower(‘GHOST‘);
+----------------+
| lower(‘GHOST‘) |
+----------------+
| ghost |
+----------------+
注:以上的那些操作都是没有改变表内容的。
格式
replace(列名,’abc’, ‘xyz’)
就是把abc 替换成xyz
mysql> select * from t1 where id<6;
+------+--------+----------+---------+
| id | name | addr | jixiao |
+------+--------+----------+---------+
| 1 | 三毛 | beijing | 550.00 |
| 2 | liu | shanghai | 300.00 |
| 3 | NULL | guangxi | -210.55 |
| 4 | meng | nanjing | NULL |
| 5 | wang | NULL | -532.30 |
+------+--------+----------+---------+
mysql> select id,name,replace(addr,‘bei‘,‘nan‘) from t1 where id<6;
+------+--------+---------------------------+
| id | name | replace(addr,‘bei‘,‘nan‘) |
+------+--------+---------------------------+
| 1 | 三毛 | nanjing |
| 2 | liu | shanghai |
| 3 | NULL | guangxi |
| 4 | meng | nanjing |
| 5 | wang | NULL |
+------+--------+---------------------------+
如果字段的某行值长度没有达到规定的长度,那么则在左边插入指定的字符:
意思就是name 这列每行的值如果长度不满10 个字符的话,那么从左边用#补齐,补足10 个字符。
格式
lpad(字段, 长度, ‘字符’)
mysql> select id,name,lpad(addr,30,‘-‘) from t1 where id<5;
+------+--------+--------------------------------+
| id | name | lpad(addr,30,‘-‘) |
+------+--------+--------------------------------+
| 1 | 三毛 | -----------------------beijing |
| 2 | liu | ----------------------shanghai |
| 3 | NULL | -----------------------guangxi |
| 4 | meng | -----------------------nanjing |
+------+--------+--------------------------------+
和lpad 意思是一样的,只是从右面填充:
mysql> select id,name,rpad(addr,30,‘-‘) from t1 where id<5;
+------+--------+--------------------------------+
| id | name | rpad(addr,30,‘-‘) |
+------+--------+--------------------------------+
| 1 | 三毛 | beijing----------------------- |
| 2 | liu | shanghai---------------------- |
| 3 | NULL | guangxi----------------------- |
| 4 | meng | nanjing----------------------- |
+------+--------+--------------------------------+
可以把某列左边的空格给取消去除。
从这里我们可以看到name=张三这行里,张三两边都是有两个空格的
Ltrim 的作用就是去除左边的空格:
mysql> create table t2 (name varchar(15));
mysql> insert into t2 values (‘ mk ‘);
mysql> select * from t2;
+------------+
| name |
+------------+
| mk |
+------------+
mysql> select name,concat(‘#‘,name,‘#‘) from t2;
+------------+----------------------+
| name | concat(‘#‘,name,‘#‘) |
+------------+----------------------+
| mk | # mk # |
+------------+----------------------+
mysql> select name,concat(‘#‘,ltrim(name),‘#‘) from t2;
+------------+-----------------------------+
| name | concat(‘#‘,ltrim(name),‘#‘) |
+------------+-----------------------------+
| mk | #mk # |
+------------+-----------------------------+
mysql> select name,concat(‘#‘,rtrim(name),‘#‘) from t2;
+------------+-----------------------------+
| name | concat(‘#‘,rtrim(name),‘#‘) |
+------------+-----------------------------+
| mk | # mk# |
+------------+-----------------------------+
把两边的空格全部都去除。
mysql> select name,concat(‘#‘,ltrim(rtrim(name)),‘#‘) from t2;
+------------+------------------------------------+
| name | concat(‘#‘,ltrim(rtrim(name)),‘#‘) |
+------------+------------------------------------+
| mk | #mk# |
+------------+------------------------------------+
left(字段, 字数)
从字段左边开始,去规定长度的字符
疑问:这里为什么没有取出来值?取出来两个空格罢了
mysql> select addr,left(addr,2) from t1 where id>10;
+-----------+--------------+
| addr | left(addr,2) |
+-----------+--------------+
| tianjin | ti |
| hennan | he |
| 上海市 | 上海 |
| beijing | be |
+-----------+--------------+
如果有截取的去求的话,那么就可以考虑left 或者right
以上是关于字符串的函数.
mysql> select addr,right(addr,2) from t1 where id>10;
+-----------+---------------+
| addr | right(addr,2) |
+-----------+---------------+
| tianjin | in |
| hennan | an |
| 上海市 | 海市 |
| beijing | ng |
+-----------+---------------+
本文出自 “果然收到” 博客,请务必保留此出处http://8850775.blog.51cto.com/8840775/1854726
原文地址:http://8850775.blog.51cto.com/8840775/1854726