时间函数
函数名称 | 描述 |
current_data() | 当前日期 |
current_time() | 当前时间 |
current_timetamp() | 当前时间戳 |
data(datatime) | 返回datatime参数的日期部分 |
data_add(date,interval d_value_type) | 在date中添加日期或者时间,interval后的数值单位可以是:year minute second day |
date_sub(date,interval d_value_type) | 在date中减去日期或者时间,interval后的数值单位可以是:year minute second day |
datediff(date1,date2) | 两个日期的差,单位是天 |
now() | 当前日期时间 |
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-02-29 |
+----------------+
1 row in set (0.00 sec)
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 12:31:11 |
+----------------+
1 row in set (0.00 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-02-29 12:31:25 |
+---------------------+
1 row in set (0.00 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-02-29 12:31:28 |
+---------------------+
1 row in set (0.00 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-02-29 12:31:32 |
+---------------------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2024-02-29 12:31:40 |
+---------------------+
1 row in set (0.00 sec)
mysql> select date('1949-10-01 00:00:00');
+-----------------------------+
| date('1949-10-01 00:00:00') |
+-----------------------------+
| 1949-10-01 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select time('1949-10-01 00:00:00');
+-----------------------------+
| time('1949-10-01 00:00:00') |
+-----------------------------+
| 00:00:00 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2024-02-29 12:32:53 |
+---------------------+
1 row in set (0.00 sec)
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2024-02-29 |
+-------------+
1 row in set (0.00 sec)
mysql> select time(now());
+-------------+
| time(now()) |
+-------------+
| 12:37:10 |
+-------------+
1 row in set (0.00 sec)
mysql> select date_add('2050-01-01',interval 10 day);
+----------------------------------------+
| date_add('2050-01-01',interval 10 day) |
+----------------------------------------+
| 2050-01-11 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2050-01-01',interval 40 day);
+----------------------------------------+
| date_add('2050-01-01',interval 40 day) |
+----------------------------------------+
| 2050-02-10 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add(now(),interval 10 day);
+---------------------------------+
| date_add(now(),interval 10 day) |
+---------------------------------+
| 2024-03-10 12:34:04 |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select date_add(now(),interval 10 minute);
+------------------------------------+
| date_add(now(),interval 10 minute) |
+------------------------------------+
| 2024-02-29 12:44:20 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2024-02-29 12:34:30 |
+---------------------+
1 row in set (0.00 sec)
mysql> select datediff('2017-10-10','2016-09-01');
+-------------------------------------+
| datediff('2017-10-10','2016-09-01') |
+-------------------------------------+
| 404 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> select datediff('2010-10-10','2016-09-01');
+-------------------------------------+
| datediff('2010-10-10','2016-09-01') |
+-------------------------------------+
| -2153 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> select datediff(now(),'1949-10-01');
+------------------------------+
| datediff(now(),'1949-10-01') |
+------------------------------+
| 27179 |
+------------------------------+
1 row in set (0.00 sec)
mysql>
时间函数案例
创建一个表记录生日信息
mysql> system clear
mysql> create table tmp(
-> id bigint primary key auto_increment,
-> birthday date not null
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc tmp;
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| birthday | date | NO | | NULL | |
+----------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+---------------------+
| Tables_in_d1 |
+---------------------+
| duplicate_table |
| exam_result |
| for_delete |
| for_truncate |
| old_duplicate_table |
| students |
| test_null |
| tmp |
+---------------------+
8 rows in set (0.00 sec)
mysql> select *from tmp;
Empty set (0.00 sec)
mysql> insert into tmp(birthday) values ('1980-01-01');
Query OK, 1 row affected (0.01 sec)
mysql> insert into tmp(birthday) values ('1990-01-01');
Query OK, 1 row affected (0.01 sec)
mysql> select *from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1980-01-01 |
| 2 | 1990-01-01 |
+----+------------+
2 rows in set (0.00 sec)
mysql> insert into tmp(birthday) values('2024-02-29');
Query OK, 1 row affected (0.01 sec)
mysql> insert into tmp(birthday) values(current_date());
Query OK, 1 row affected (0.01 sec)
mysql> insert into tmp(birthday) values(current_time());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 12:43:41 |
+----------------+
1 row in set (0.00 sec)
mysql> insert into tmp(birthday) values(current_timestamp());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-02-29 12:44:32 |
+---------------------+
1 row in set (0.00 sec)
mysql> insert into tmp(birthday) values(date(current_timestamp()));
Query OK, 1 row affected (0.01 sec)
mysql> select *from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1980-01-01 |
| 2 | 1990-01-01 |
| 3 | 2024-02-29 |
| 4 | 2024-02-29 |
| 5 | 2024-02-29 |
| 6 | 2024-02-29 |
| 7 | 2024-02-29 |
+----+------------+
7 rows in set (0.00 sec)
mysql>
创建一个留言板
mysql> create table msg(
-> id bigint primary key auto_increment,
-> content varchar(100) not null,
-> sendtime datetime
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc msg;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| content | varchar(100) | NO | | NULL | |
| sendtime | datetime | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into msg (content,sendtime )values ('纸上得来终觉浅,绝知此事要躬行',now());
Query OK, 1 row affected (0.01 sec)
mysql> select *from msg;
+----+-----------------------------------------------+---------------------+
| id | content | sendtime |
+----+-----------------------------------------------+---------------------+
| 1 | 纸上得来终觉浅,绝知此事要躬行 | 2024-02-29 12:49:40 |
+----+-----------------------------------------------+---------------------+
1 row in set (0.00 sec)
mysql> insert into msg (content,sendtime) values('恐惊天上人',now());
Query OK, 1 row affected (0.00 sec)
mysql> select*from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1980-01-01 |
| 2 | 1990-01-01 |
| 3 | 2024-02-29 |
| 4 | 2024-02-29 |
| 5 | 2024-02-29 |
| 6 | 2024-02-29 |
| 7 | 2024-02-29 |
+----+------------+
7 rows in set (0.00 sec)
mysql> select*from msg;
+----+-----------------------------------------------+---------------------+
| id | content | sendtime |
+----+-----------------------------------------------+---------------------+
| 1 | 纸上得来终觉浅,绝知此事要躬行 | 2024-02-29 12:49:40 |
| 2 | 恐惊天上人 | 2024-02-29 12:51:03 |
+----+-----------------------------------------------+---------------------+
2 rows in set (0.00 sec)
mysql> select content,sendtime from msg;
+-----------------------------------------------+---------------------+
| content | sendtime |
+-----------------------------------------------+---------------------+
| 纸上得来终觉浅,绝知此事要躬行 | 2024-02-29 12:49:40 |
| 恐惊天上人 | 2024-02-29 12:51:03 |
+-----------------------------------------------+---------------------+
2 rows in set (0.00 sec)
mysql> select content,date(sendtime) from msg;
+-----------------------------------------------+----------------+
| content | date(sendtime) |
+-----------------------------------------------+----------------+
| 纸上得来终觉浅,绝知此事要躬行 | 2024-02-29 |
| 恐惊天上人 | 2024-02-29 |
+-----------------------------------------------+----------------+
2 rows in set (0.00 sec)
mysql>
查询在两分钟之内发布的帖子
mysql> system clear
mysql> select content ,sendtime from msg where sendtime >date_sub(now(),interval 2 minute);
Empty set (0.00 sec)
mysql> insert into msg(content ,sendtime ) values ('会当凌绝顶',now());
Query OK, 1 row affected (0.00 sec)
mysql> select content ,sendtime from msg where sendtime >date_sub(now(),interval 2 minute);
+-----------------+---------------------+
| content | sendtime |
+-----------------+---------------------+
| 会当凌绝顶 | 2024-02-29 12:54:58 |
+-----------------+---------------------+
1 row in set (0.01 sec)
mysql> insert into msg(content ,sendtime ) values ('会当凌绝顶',now());
Query OK, 1 row affected (0.00 sec)
mysql> insert into msg(content ,sendtime ) values ('会当凌绝顶',now());
Query OK, 1 row affected (0.01 sec)
mysql> select content ,sendtime from msg where sendtime >date_sub(now(),interval 2 minute);
+-----------------+---------------------+
| content | sendtime |
+-----------------+---------------------+
| 会当凌绝顶 | 2024-02-29 12:54:58 |
| 会当凌绝顶 | 2024-02-29 12:55:31 |
| 会当凌绝顶 | 2024-02-29 12:55:33 |
+-----------------+---------------------+
3 rows in set (0.00 sec)
mysql>
字符串函数
charset(str) | 返回字符串字符集 |
concat(string2[,...]) | 连接字符串 |
instr(string,substring) | 返回substring在string中出现的位置,没有返回0 |
ucase(string2) | 转换成大写 |
lcase(string2) | 转换成小写 |
left(string2 ,length) | 从string2中的左边取length个字符 |
length(string) | string长度 |
replace(str,search_str,replace_str) | 在str中用erplace_str替换search_str |
strcmp(string1,string2) | 逐字符比较两字符串的大小 |
substring(str,position[,length]) | 从str的position开始,取length个字符 |
ltrim(string) | 去除左空格 |
rtrim(string) | 去除右空格 |
trim(string) | 去除左右空格 |
charset
mysql> select charset('asasds');
+-------------------+
| charset('asasds') |
+-------------------+
| utf8 |
+-------------------+
1 row in set (0.00 sec)
mysql> select charset('中国');
+-------------------+
| charset('中国') |
+-------------------+
| utf8 |
+-------------------+
1 row in set (0.00 sec)
mysql> desc emp;
+----------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------------------+------+-----+---------+-------+
| empno | int(6) unsigned zerofill | NO | | NULL | |
| ename | varchar(10) | YES | | NULL | |
| job | varchar(9) | YES | | NULL | |
| mgr | int(4) unsigned zerofill | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | decimal(7,2) | YES | | NULL | |
| comm | decimal(7,2) | YES | | NULL | |
| deptno | int(2) unsigned zerofill | YES | | NULL | |
+----------+--------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
+----------------+
14 rows in set (0.00 sec)
mysql>
concat
mysql> select concat('a','b');
+-----------------+
| concat('a','b') |
+-----------------+
| ab |
+-----------------+
1 row in set (0.00 sec)
mysql> select concat('a','b','c');
+---------------------+
| concat('a','b','c') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.00 sec)
mysql> select concat('a','b','c') as res;
+------+
| res |
+------+
| abc |
+------+
1 row in set (0.00 sec)
mysql> select concat('a','b','c',123,3.14) as res;
+------------+
| res |
+------------+
| abc1233.14 |
+------------+
1 row in set (0.00 sec)
mysql>
instr
mysql> select instr('abcdef1234gfd','1234');
+-------------------------------+
| instr('abcdef1234gfd','1234') |
+-------------------------------+
| 7 |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select instr('abcdef1234gfd','1234a');
+--------------------------------+
| instr('abcdef1234gfd','1234a') |
+--------------------------------+
| 0 |
+--------------------------------+
1 row in set (0.00 sec)
mysql>
ucase、lcase
mysql> select ucase('abcdef1234ABCDef');
+---------------------------+
| ucase('abcdef1234ABCDef') |
+---------------------------+
| ABCDEF1234ABCDEF |
+---------------------------+
1 row in set (0.00 sec)
mysql> select lcase('asdajaj1234jASHDNCAS');
+-------------------------------+
| lcase('asdajaj1234jASHDNCAS') |
+-------------------------------+
| asdajaj1234jashdncas |
+-------------------------------+
1 row in set (0.00 sec)
mysql>
left、right
mysql> select left ('abcde1234',4)
-> ;
+----------------------+
| left ('abcde1234',4) |
+----------------------+
| abcd |
+----------------------+
1 row in set (0.00 sec)
mysql> select left ('abcde1234',5);
+----------------------+
| left ('abcde1234',5) |
+----------------------+
| abcde |
+----------------------+
1 row in set (0.00 sec)
mysql> select left ('abcde1234',3);
+----------------------+
| left ('abcde1234',3) |
+----------------------+
| abc |
+----------------------+
1 row in set (0.00 sec)
mysql> select right ('abcde1234',3);
+-----------------------+
| right ('abcde1234',3) |
+-----------------------+
| 234 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select right ('abcde1234',4);
+-----------------------+
| right ('abcde1234',4) |
+-----------------------+
| 1234 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select right ('abcde1234',5);
+-----------------------+
| right ('abcde1234',5) |
+-----------------------+
| e1234 |
+-----------------------+
1 row in set (0.00 sec)
mysql>
length
mysql> select length('abcde1234');
+---------------------+
| length('abcde1234') |
+---------------------+
| 9 |
+---------------------+
1 row in set (0.00 sec)
mysql> select length('');
+------------+
| length('') |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)
mysql>
字符串函数案例
获取emp表的ename列的字符集
mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
+----------------+
14 rows in set (0.00 sec)
mysql> select charset(sal) from emp;
+--------------+
| charset(sal) |
+--------------+
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
| binary |
+--------------+
14 rows in set (0.00 sec)
mysql> select *from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)
mysql>
要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
mysql> select *from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 3 | 猪悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 6 | 孙权 | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
5 rows in set (0.00 sec)
mysql> select name ,chinese +math+english 总分,chinese,math,english from exam_result;
+-----------+--------+---------+------+---------+
| name | 总分 | chinese | math | english |
+-----------+--------+---------+------+---------+
| 唐三藏 | 288 | 134 | 98 | 56 |
| 猪悟能 | 364 | 176 | 98 | 90 |
| 曹孟德 | 297 | 140 | 90 | 67 |
| 孙权 | 291 | 140 | 73 | 78 |
| 宋公明 | 275 | 150 | 95 | 30 |
+-----------+--------+---------+------+---------+
5 rows in set (0.00 sec)
mysql> select concat(name ,chinese +math+english ,chinese,math,english)msg from exam_result;
+---------------------+
| msg |
+---------------------+
| 唐三藏2881349856 |
| 猪悟能3641769890 |
| 曹孟德2971409067 |
| 孙权2911407378 |
| 宋公明2751509530 |
+---------------------+
5 rows in set (0.00 sec)
mysql> select concat('考生姓名:',name,',总分:' ,chinese +math+english ,',语文成绩:',chinese,',数学成绩:',math,',英语成绩:',english)msg from exam_result;
+----------------------------------------------------------------------------------------------+
| msg |
+----------------------------------------------------------------------------------------------+
| 考生姓名:唐三藏,总分:288,语文成绩:134,数学成绩:98,英语成绩:56 |
| 考生姓名:猪悟能,总分:364,语文成绩:176,数学成绩:98,英语成绩:90 |
| 考生姓名:曹孟德,总分:297,语文成绩:140,数学成绩:90,英语成绩:67 |
| 考生姓名:孙权,总分:291,语文成绩:140,数学成绩:73,英语成绩:78 |
| 考生姓名:宋公明,总分:275,语文成绩:150,数学成绩:95,英语成绩:30 |
+----------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql>
求学生表中学生姓名占用的字节数
length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字符。比如:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)。
mysql> select name ,length(name) from exam_result;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| 唐三藏 | 9 |
| 猪悟能 | 9 |
| 曹孟德 | 9 |
| 孙权 | 6 |
| 宋公明 | 9 |
+-----------+--------------+
5 rows in set (0.00 sec)
mysql> select name ,length(math) from exam_result;
+-----------+--------------+
| name | length(math) |
+-----------+--------------+
| 唐三藏 | 2 |
| 猪悟能 | 2 |
| 曹孟德 | 2 |
| 孙权 | 2 |
| 宋公明 | 2 |
+-----------+--------------+
5 rows in set (0.00 sec)
mysql> select length('ab');
+--------------+
| length('ab') |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
mysql> select length('abc1');
+----------------+
| length('abc1') |
+----------------+
| 4 |
+----------------+
1 row in set (0.00 sec)
mysql> select length('abc1你好');
+----------------------+
| length('abc1你好') |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
mysql>
将emp表中所有名字中有S的替换成‘上海’
mysql> select *from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)
mysql> select ename,replace(ename,'S','上海') from emp;
+--------+-----------------------------+
| ename | replace(ename,'S','上海') |
+--------+-----------------------------+
| SMITH | 上海MITH |
| ALLEN | ALLEN |
| WARD | WARD |
| JONES | JONE上海 |
| MARTIN | MARTIN |
| BLAKE | BLAKE |
| CLARK | CLARK |
| SCOTT | 上海COTT |
| KING | KING |
| TURNER | TURNER |
| ADAMS | ADAM上海 |
| JAMES | JAME上海 |
| FORD | FORD |
| MILLER | MILLER |
+--------+-----------------------------+
14 rows in set (0.00 sec)
mysql>
截取emp表中ename字段的第二个到第三个字符
mysql> select ename from emp;
+--------+
| ename |
+--------+
| SMITH |
| ALLEN |
| WARD |
| JONES |
| MARTIN |
| BLAKE |
| CLARK |
| SCOTT |
| KING |
| TURNER |
| ADAMS |
| JAMES |
| FORD |
| MILLER |
+--------+
14 rows in set (0.00 sec)
mysql> select substring(ename,2,2),ename from emp;
+----------------------+--------+
| substring(ename,2,2) | ename |
+----------------------+--------+
| MI | SMITH |
| LL | ALLEN |
| AR | WARD |
| ON | JONES |
| AR | MARTIN |
| LA | BLAKE |
| LA | CLARK |
| CO | SCOTT |
| IN | KING |
| UR | TURNER |
| DA | ADAMS |
| AM | JAMES |
| OR | FORD |
| IL | MILLER |
+----------------------+--------+
14 rows in set (0.00 sec)
mysql>
以手写字母小写的方式显示所有员工的姓名
mysql> select ename,substring(ename,1,1),substring(ename,2) from emp;
+--------+----------------------+--------------------+
| ename | substring(ename,1,1) | substring(ename,2) |
+--------+----------------------+--------------------+
| SMITH | S | MITH |
| ALLEN | A | LLEN |
| WARD | W | ARD |
| JONES | J | ONES |
| MARTIN | M | ARTIN |
| BLAKE | B | LAKE |
| CLARK | C | LARK |
| SCOTT | S | COTT |
| KING | K | ING |
| TURNER | T | URNER |
| ADAMS | A | DAMS |
| JAMES | J | AMES |
| FORD | F | ORD |
| MILLER | M | ILLER |
+--------+----------------------+--------------------+
14 rows in set (0.00 sec)
mysql> select ename,lcase(substring(ename,1,1)),substring(ename,2) from emp;
+--------+-----------------------------+--------------------+
| ename | lcase(substring(ename,1,1)) | substring(ename,2) |
+--------+-----------------------------+--------------------+
| SMITH | s | MITH |
| ALLEN | a | LLEN |
| WARD | w | ARD |
| JONES | j | ONES |
| MARTIN | m | ARTIN |
| BLAKE | b | LAKE |
| CLARK | c | LARK |
| SCOTT | s | COTT |
| KING | k | ING |
| TURNER | t | URNER |
| ADAMS | a | DAMS |
| JAMES | j | AMES |
| FORD | f | ORD |
| MILLER | m | ILLER |
+--------+-----------------------------+--------------------+
14 rows in set (0.00 sec)
mysql> select ename,concat(lcase(substring(ename,1,1)),substring(ename,2)) from emp;
+--------+--------------------------------------------------------+
| ename | concat(lcase(substring(ename,1,1)),substring(ename,2)) |
+--------+--------------------------------------------------------+
| SMITH | sMITH |
| ALLEN | aLLEN |
| WARD | wARD |
| JONES | jONES |
| MARTIN | mARTIN |
| BLAKE | bLAKE |
| CLARK | cLARK |
| SCOTT | sCOTT |
| KING | kING |
| TURNER | tURNER |
| ADAMS | aDAMS |
| JAMES | jAMES |
| FORD | fORD |
| MILLER | mILLER |
+--------+--------------------------------------------------------+
14 rows in set (0.00 sec)
mysql>
清理字符串左、右空格
mysql> select ltrim(' 你好 ');
+----------------------------+
| ltrim(' 你好 ') |
+----------------------------+
| 你好 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select ltrim(' 你好 ') as res;
+--------------+
| res |
+--------------+
| 你好 |
+--------------+
1 row in set (0.00 sec)
mysql> select rtrim(' 你好 ') as res;
+-------------+
| res |
+-------------+
| 你好 |
+-------------+
1 row in set (0.00 sec)
mysql> select trim(' 你好 ') as res;
+--------+
| res |
+--------+
| 你好 |
+--------+
1 row in set (0.00 sec)
mysql> select trim(' 你好 hello ') as res;
+---------------------+
| res |
+---------------------+
| 你好 hello |
+---------------------+
1 row in set (0.00 sec)
mysql> select ltrim(' 你好 hello ') as res;
+-----------------------------------+
| res |
+-----------------------------------+
| 你好 hello |
+-----------------------------------+
1 row in set (0.00 sec)
mysql> select rtrim(' 你好 hello ') as res;
+--------------------------+
| res |
+--------------------------+
| 你好 hello |
+--------------------------+
1 row in set (0.00 sec)
mysql>
结尾
最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。
同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。
谢谢您的支持,期待与您在下一篇文章中再次相遇!