博主花了两天两夜,整理出一些 SQL 语句的高级使用方法。SQL语句的熟练使用,在平时的运维工作中可以提供不小的帮助,尤其是在一些规模较小的公司,运维身兼数职,可能会有不少数据库的相关工作。本章将从不同的方面出发介绍 SQL 语句的高级运用方法。
在对 MySQL 数据库的增、删、改、查操作有一定了解之后,就可以学习一些 SQL 语句的高级使用方法。SQL语句的熟练使用,在平时的运维工作中可以提供不小的帮助,尤其是在一些规模较小的公司,运维身兼数职,可能会有不少数据库的相关工作。本章将从不同的方面出发介绍 SQL 语句的高级运用方法。
本数据库环境为MySQL5.6
SELECT column1, column2,....FROM table_name ORDER BY column1,column2,...ASC|DESC;
mysql> create database tt;
mysql> use tt;
mysql> create table tt (id int(10) not null primary key auto_increment,name char(20) not null,score decimal(5,2),address varchar(40)default '未知')engine=innodb;
mysql> desc tt;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | 未知 | |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> insert into tt (name,score,address) values ('wangwu',68,'beijing'),('lisi',9njing');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into tt (name,score,address) values ('zhangsan',74,'beijing'),('shang78,'hangzhou');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
#写入数据
mysql> select * from tt;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | wangwu | 68.00 | beijing |
| 2 | lisi | 90.00 | nanjing |
| 3 | zhangsan | 74.00 | beijing |
| 4 | shanghai | 78.00 | hangzhou |
+----+----------+-------+----------+
4 rows in set (0.02 sec)
mysql> select name,score from tt where score>=70;
+----------+-------+
| name | score |
+----------+-------+
| lisi | 90.00 |
| zhangsan | 74.00 |
| shanghai | 78.00 |
+----------+-------+
3 rows in set (0.00 sec)
#筛选大于70分的进行升序排序 【对score字段排序】
mysql> select name,score from tt where score>=70 order by score; #ASC可省略,升序
+----------+-------+
| name | score |
+----------+-------+
| zhangsan | 74.00 |
| shanghai | 78.00 |
| lisi | 90.00 |
+----------+-------+
3 rows in set (0.00 sec)
#筛选大于70分的进行降序排序 【对score字段排序】
mysql> select name,score from tt where score>=70 order by score desc; #降序
+----------+-------+
| name | score |
+----------+-------+
| lisi | 90.00 |
| shanghai | 78.00 |
| zhangsan | 74.00 |
+----------+-------+
3 rows in set (0.00 sec)
这边我们再次插入新的数据内容
mysql> insert into tt(name,score,address) values('tom',66,'wuxi'),('jerry',77,'hangzhou');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from tt;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | wangwu | 68.00 | beijing |
| 2 | lisi | 90.00 | nanjing |
| 3 | zhangsan | 74.00 | beijing |
| 4 | shanghai | 78.00 | hangzhou |
| 5 | tom | 66.00 | wuxi |
| 6 | jerry | 77.00 | hangzhou |
+----+----------+-------+----------+
6 rows in set (0.01 sec)
mysql> select id,name,score from tt where score>=70 order by score ASC;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 3 | zhangsan | 74.00 |
| 6 | jerry | 77.00 |
| 4 | shanghai | 78.00 |
| 2 | lisi | 90.00 |
+----+----------+-------+
4 rows in set (0.00 sec)
mysql> select id,name,score from tt where score>=70 order by score DESC;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 2 | lisi | 90.00 |
| 4 | shanghai | 78.00 |
| 6 | jerry | 77.00 |
| 3 | zhangsan | 74.00 |
+----+----------+-------+
4 rows in set (0.00 sec)
mysql> update tt set score=66 where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select id,name,score from tt where score>=70 order by score desc;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 4 | shanghai | 78.00 |
| 6 | jerry | 77.00 |
| 3 | zhangsan | 74.00 |
+----+----------+-------+
3 rows in set (0.00 sec)
#更改表中数据如下
mysql> select * from tt;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | wangwu | 80.00 | beijing |
| 2 | lisi | 66.00 | nanjing |
| 3 | zhangsan | 80.00 | beijing |
| 4 | shanghai | 78.00 | hangzhou |
| 5 | tom | 80.00 | wuxi |
| 6 | jerry | 77.00 | hangzhou |
+----+----------+-------+----------+
6 rows in set (0.00 sec)
mysql> select id,name,score from tt where score>=70 order by score desc,id desc ;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 5 | tom | 80.00 |
| 3 | zhangsan | 80.00 |
| 1 | wangwu | 80.00 |
| 4 | shanghai | 78.00 |
| 6 | jerry | 77.00 |
+----+----------+-------+
5 rows in set (0.00 sec)
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现。
GROUP BY 从字面上看,是以 BY 后面的内容对查询出的数据进行分组,就是将一个“数据集”划分成若干个“小区域”,然后针对这些个“小区域”进行数据处理。
SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator value GROUP BY column_name;
count (字段名称) 计数 函数
count (*) 技术
sum (*) 求和
avg (*) 平均值
max (*) 最大
min (*) 最小值
统计70跟80分的人数
mysql> select * from tt;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | wangwu | 80.00 | beijing |
| 2 | lisi | 66.00 | nanjing |
| 3 | zhangsan | 80.00 | beijing |
| 4 | shanghai | 78.00 | hangzhou |
| 5 | tom | 80.00 | wuxi |
| 6 | jerry | 77.00 | hangzhou |
+----+----------+-------+----------+
6 rows in set (0.00 sec)
mysql> select count(name),score from tt where score >=70 group by score;
+-------------+-------+
| count(name) | score |
+-------------+-------+
| 1 | 77.00 |
| 1 | 78.00 |
| 3 | 80.00 |
+-------------+-------+
3 rows in set (0.00 sec)
求班级的学生的平均成绩
mysql> select avg(score) from tt;
+------------+
| avg(score) |
+------------+
| 76.833333 |
+------------+
1 row in set (0.00 sec)
mysql> select count(name),score from tt where score >=70 group by score order by score desc;
+-------------+-------+
| count(name) | score |
+-------------+-------+
| 3 | 80.00 |
| 1 | 78.00 |
| 1 | 77.00 |
+-------------+-------+
3 rows in set (0.00 sec)
mysql> select count(name),score from tt where score >=70 group by score order by score ;
+-------------+-------+
| count(name) | score |
+-------------+-------+
| 1 | 77.00 |
| 1 | 78.00 |
| 3 | 80.00 |
+-------------+-------+
3 rows in set (0.00 sec)
在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录。有时候仅需要返回第一行或者前几行,这时候就需要用到 LIMIT 子句。
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的位置偏移量是 0,第二条是 1,以此类推。第二个参数是设置返回记录行的最大数目。
SELECT column1,column2,...FROM table_name LIMIT[offset,] number;
number:返回记录行的最大数目
[offset,]:位置偏移量,从0开始
mysql> select * from tt limit 3;
+----+----------+-------+---------+
| id | name | score | address |
+----+----------+-------+---------+
| 1 | wangwu | 80.00 | beijing |
| 2 | lisi | 66.00 | nanjing |
| 3 | zhangsan | 80.00 | beijing |
+----+----------+-------+---------+
3 rows in set (0.00 sec)
mysql> select * from tt limit 3,2;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 4 | shanghai | 78.00 | hangzhou |
| 5 | tom | 80.00 | wuxi |
+----+----------+-------+----------+
2 rows in set (0.00 sec)
mysql> insert into tt (name,score,address) values ('aa',87,'shanghai'),('shuaige',76,'hangzgzhou');
mysql> select * from tt;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | wangwu | 80.00 | beijing |
| 2 | lisi | 66.00 | nanjing |
| 3 | zhangsan | 80.00 | beijing |
| 4 | shanghai | 78.00 | hangzhou |
| 5 | tom | 80.00 | wuxi |
| 6 | jerry | 77.00 | hangzhou |
| 7 | aa | 87.00 | shanghai |
| 8 | shuaige | 76.00 | hangzhou |
+----+----------+-------+----------+
8 rows in set (0.00 sec)
mysql> select * from tt order by score desc limit 3;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 7 | aa | 87.00 | shanghai |
| 1 | wangwu | 80.00 | beijing |
| 3 | zhangsan | 80.00 | beijing |
+----+----------+-------+----------+
3 rows in set (0.00 sec)
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增 强可读性。
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s)FROM table_name ASalias_name;
示例
mysql> select count(*) from tt;
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) as 数量 from tt;
+--------+
| 数量 |
+--------+
| 8 |
+--------+
1 row in set (0.00 sec)
mysql> create table gg (id int(10) not null primary key auto_increment,gg_name varchar(10) not null);
Query OK, 0 rows affected (0.07 sec)
mysql> show tables;
+--------------+
| Tables_in_tt |
+--------------+
| gg |
| tt |
+--------------+
2 rows in set (0.00 sec)
mysql> desc gg;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| gg_name | varchar(10) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> insert into gg (gg_name) values ('乔治'),('佩奇'),('苏西');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from gg;
+----+---------+
| id | gg_name |
+----+---------+
| 1 | 乔治 |
| 2 | 佩奇 |
| 3 | 苏西 |
+----+---------+
3 rows in set (0.00 sec)
mysql> alter table tt add column hobby int(3) not null;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tt;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | 未知 | |
| hobby | int(3) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 0 |
| 2 | lisi | 66.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 0 |
| 4 | shanghai | 78.00 | hangzhou | 0 |
| 5 | tom | 80.00 | wuxi | 0 |
| 6 | jerry | 77.00 | hangzhou | 0 |
| 7 | aa | 87.00 | shanghai | 0 |
| 8 | shuaige | 76.00 | hangzhou | 0 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
定义了不允许为空 null 这边显示跌是默认值
如果定义的是允许为空 就是显示的是null 什么都没雨
数据库中’ ':是空的字符串 是有值的
null:是什么都没有 没有值
mysql> update tt set hobby=1 where score >=75;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 1 |
| 2 | lisi | 66.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 1 |
| 4 | shanghai | 78.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 1 |
| 6 | jerry | 77.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 1 |
| 8 | shuaige | 76.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
mysql> update tt set hobby=2 where score>=80;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 66.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 78.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 77.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 76.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
inner join 【内连接】
mysql> select * from tt inner join gg on tt.hobby=gg.id
-> ;
+----+----------+-------+----------+-------+----+---------+
| id | name | score | address | hobby | id | gg_name |
+----+----------+-------+----------+-------+----+---------+
| 1 | wangwu | 80.00 | beijing | 2 | 2 | 佩奇 |
| 3 | zhangsan | 80.00 | beijing | 2 | 2 | 佩奇 |
| 4 | shanghai | 78.00 | hangzhou | 1 | 1 | 乔治 |
| 5 | tom | 80.00 | wuxi | 2 | 2 | 佩奇 |
| 6 | jerry | 77.00 | hangzhou | 1 | 1 | 乔治 |
| 7 | aa | 87.00 | shanghai | 2 | 2 | 佩奇 |
| 8 | shuaige | 76.00 | hangzhou | 1 | 1 | 乔治 |
+----+----------+-------+----------+-------+----+---------+
7 rows in set (0.00 sec)
mysql> select tt.name,gg.gg_name from tt inner join gg on tt.hobby=gg.id;
+----------+---------+
| name | gg_name |
+----------+---------+
| wangwu | 佩奇 |
| zhangsan | 佩奇 |
| shanghai | 乔治 |
| tom | 佩奇 |
| jerry | 乔治 |
| aa | 佩奇 |
| shuaige | 乔治 |
+----------+---------+
7 rows in set (0.00 sec)
mysql> select T.name,G.gg_name from tt as T inner join gg as G on T.hobby=G.id;
+----------+---------+
| name | gg_name |
+----------+---------+
| wangwu | 佩奇 |
| zhangsan | 佩奇 |
| shanghai | 乔治 |
| tom | 佩奇 |
| jerry | 乔治 |
| aa | 佩奇 |
| shuaige | 乔治 |
+----------+---------+
7 rows in set (0.00 sec)
#取消自定义
mysql> select T.name,G.gg_name from tt T inner join gg G on T.hobby=G.id;
+----------+---------+
| name | gg_name |
+----------+---------+
| wangwu | 佩奇 |
| zhangsan | 佩奇 |
| shanghai | 乔治 |
| tom | 佩奇 |
| jerry | 乔治 |
| aa | 佩奇 |
| shuaige | 乔治 |
+----------+---------+
7 rows in set (0.00 sec)
mysql> select T.name 姓名,G.gg_name 角色 from tt T inner join gg G on T.hobby=G.id;
+----------+--------+
| 姓名 | 角色 |
+----------+--------+
| wangwu | 佩奇 |
| zhangsan | 佩奇 |
| shanghai | 乔治 |
| tom | 佩奇 |
| jerry | 乔治 |
| aa | 佩奇 |
| shuaige | 乔治 |
+----------+--------+
7 rows in set (0.00 sec)
1.5:通配符
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。 通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。
%表示零个、一个或多个
_表示单个字符
mysql> select name,score from tt where name like 'z_______';
+----------+-------+
| name | score |
+----------+-------+
| zhangsan | 80.00 |
+----------+-------+
1 row in set (0.00 sec)
mysql> select name,score from tt where name like 'z%';
+----------+-------+
| name | score |
+----------+-------+
| zhangsan | 80.00 |
+----------+-------+
1 row in set (0.00 sec)
mysql> select name,score from tt where name like 's_%';
+----------+-------+
| name | score |
+----------+-------+
| shanghai | 78.00 |
| shuaige | 76.00 |
+----------+-------+
2 rows in set (0.00 sec)
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE 中也同样适用。在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层 嵌套。
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 66.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 78.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 77.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 76.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
mysql> select * from gg;
+----+---------+
| id | gg_name |
+----+---------+
| 1 | 乔治 |
| 2 | 佩奇 |
| 3 | 苏西 |
+----+---------+
3 rows in set (0.00 sec)
#筛选佩奇的兴趣爱好组的人
mysql> select name,hobby from tt where hobby in(select id from gg where gg_name='佩奇');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
4 rows in set (0.00 sec)
方法二:后面输出的结果赋予了前面的值
mysql> select name,hobby from tt where hobby=(select id from gg where gg_name='佩奇');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
4 rows in set (0.00 sec)
筛选出乔治与佩奇兴趣爱好 的人
mysql> select name,hobby from tt where hobby in (select id from gg);
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| zhangsan | 2 |
| shanghai | 1 |
| tom | 2 |
| jerry | 1 |
| aa | 2 |
| shuaige | 1 |
+----------+-------+
7 rows in set (0.00 sec)
查看佩奇兴趣爱好的人
mysql> select name,hobby from tt where hobby>(select id from gg where gg_name='乔治');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
4 rows in set (0.00 sec)
mysql> select name,hobby from tt where hobby in(select id from gg where gg_name='乔治');
+----------+-------+
| name | hobby |
+----------+-------+
| shanghai | 1 |
| jerry | 1 |
| shuaige | 1 |
+----------+-------+
3 rows in set (0.00 sec)
mysql> select name,hobby from tt where hobby in(select id from gg where gg_name='佩奇');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
4 rows in set (0.00 sec)
mysql> select name,hobby from tt where hobby=(select id from gg where gg_name='佩奇');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
4 rows in set (0.00 sec)
mysql> select name,hobby from tt where hobby!=(select id from gg where gg_name='乔治');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| lisi | 0 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
5 rows in set (0.00 sec)
mysql> select name,hobby from tt where hobby<>(select id from gg where gg_name='乔治');
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| lisi | 0 |
| zhangsan | 2 |
| tom | 2 |
| aa | 2 |
+----------+-------+
5 rows in set (0.00 sec)
#总结:经过测试发现mysql中用<>与!=都是可以的,但sqlserver中不识别!=,所以建议用<>
mysql> select 1+5;
+-----+
| 1+5 |
+-----+
| 6 |
+-----+
1 row in set (0.01 sec)
mysql> select 1*5;
+-----+
| 1*5 |
+-----+
| 5 |
+-----+
1 row in set (0.00 sec)
mysql> select 1=5;
+-----+
| 1=5 |
+-----+
| 0 |
+-----+
1 row in set (0.00 sec)
mysql> select 1!=5;
+------+
| 1!=5 |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> <> or != 用于判断数字,字符串,表达式不相等的判断,如果不相等,返回值为1,否则返回值为0.
mysql> create table xi like tt;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+--------------+
| Tables_in_tt |
+--------------+
| gg |
| tt |
| xi |
+--------------+
3 rows in set (0.00 sec)
mysql> select * from xi;
Empty set (0.00 sec)
mysql> insert into xi select * from tt where id in (select id from tt where address='beijing');
Query OK, 2 rows affected, 1 warning (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 1
mysql> select * from xi;
+----+----------+-------+---------+-------+
| id | name | score | address | hobby |
+----+----------+-------+---------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 3 | zhangsan | 80.00 | beijing | 2 |
+----+----------+-------+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 66.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 78.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 77.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 76.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
mysql> update tt set score=score+5 where score<80;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 71.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 82.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
mysql> select name,score from tt where exists (select id from gg where gg_name='佩奇');
+----------+-------+
| name | score |
+----------+-------+
| wangwu | 80.00 |
| lisi | 71.00 |
| zhangsan | 80.00 |
| shanghai | 83.00 |
| tom | 80.00 |
| jerry | 82.00 |
| aa | 87.00 |
| shuaige | 81.00 |
+----------+-------+
8 rows in set (0.00 sec)
mysql> select name,score from tt where exists (select id from gg where gg_name='任务');
Empty set (0.00 sec)
空值长度为0,不占空间;NULL值的长度为NULL,占用空间
.IS NULL无法判断空值
空值使用“="或者“<>"来处理
.COUNT()计算时,NULL会忽略,空值会加入计算
NULL里面没有任何值 ,占用一部分空间
示例
mysql> create table num (id int(4) not null primary key auto_increment,name char(10));
Query OK, 0 rows affected (0.02 sec)
mysql> desc num;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into num (id,name) values (2,'tom');
Query OK, 1 row affected (0.01 sec)
mysql> select * from num;
+----+------+
| id | name |
+----+------+
| 2 | tom |
+----+------+
1 row in set (0.00 sec)
mysql> insert into num (id) values (3);
Query OK, 1 row affected (0.01 sec)
mysql> select * from num;
+----+------+
| id | name |
+----+------+
| 2 | tom |
| 3 | NULL |
2 rows in set (0.00 sec)
#在向表内插入记录或者更新记录时,如果该字段没有 NOT NULL 并且没有值,这时候新记录的该字段将被保存为 NULL
mysql> select count(id) from num;
+-----------+
| count(id) |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
mysql> select count(name) from num;
+-------------+
| count(name) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
#COUNT()计算时,NULL会忽略,空值会加入计算`
#插入空值
mysql> insert into num (id,name) values (4,'');
mysql> insert into num (id,name) values (4,'');
Query OK, 1 row affected (0.01 sec)
mysql> select * from num;
+----+------+
| id | name |
+----+------+
| 2 | tom |
| 3 | NULL |
| 4 | |
+----+------+
3 rows in set (0.00 sec)
mysql> select count(name) from num;
+-------------+
| count(name) |
+-------------+
| 2 |
+-------------+
1 row in set (0.01 sec)
#可以看出空值会被加入计算 null不会
mysql> select * from num where name is not null;
+----+------+
| id | name |
+----+------+
| 2 | tom |
| 4 | |
+----+------+
2 rows in set (0.00 sec)
mysql> select * from num where name is null;
+----+------+
| id | name |
+----+------+
| 3 | NULL |
+----+------+
1 row in set (0.00 sec)
MySQL 正则表达式通常是在检索数据库记录的时候,根据指定的匹配模式匹配记录中符合要求的特殊字符串。MySQL 的正则表达式使用 REGEXP 这个关键字来指定正则表达式的匹配模式。
匹配模式 | 描述 | 实例 |
---|---|---|
^ | 匹配文本的开始字符 | ‘^bd’ 匹配以 bd 开头的字符串 |
$ | 匹配文本的结束字符 | ‘qn$’ 匹配以 qn 结尾的字符串 |
. | 匹配任何单个字符 | ‘s.t’ 匹配任何s 和t 之间有一个字符的字符串 |
* | 匹配零个或多个在它前面的字符 | ‘fo*t’ 匹配 t 前面有任意个 o |
+ | 匹配前面的字符 1 次或多次 | ‘hom+’ 匹配以 ho 开头,后面至少一个m 的字符串 |
字符串 | 匹配包含指定的字符串 | ‘clo’ 匹配含有 clo 的字符串 |
p1 | p2 | 匹配 p1 或 p2 |
[…] | 匹配字符集合中的任意一个字符 | ‘[abc]’ 匹配 a 或者 b 或者 c |
[^…] | 匹配不在括号中的任何字符 | ‘[^ab]’ 匹配不包含 a 或者 b 的字符串 |
{n} | 匹配前面的字符串 n 次 | ‘g{2}’ 匹配含有 2 个 g 的字符串 |
{n,m} | 匹配前面的字符串至少 n 次,至多m 次 | ‘f{1,3}’ 匹配 f 最少 1 次,最多 3 次 |
了解正则表达式的匹配规则之后,就可以将这些规则技巧应用于 SQL 语句中,从而可以更准确、更快速的查出所需的记录。下面通过示例的方式详细介绍 MySQL 正则表达式的使用方法。
示例:
mysql> select * from tt where name regexp '^wa';
+----+--------+-------+---------+-------+
| id | name | score | address | hobby |
+----+--------+-------+---------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
+----+--------+-------+---------+-------+
1 row in set (0.00 sec)
mysql> select * from tt where name regexp 'm$';
+----+------+-------+---------+-------+
| id | name | score | address | hobby |
+----+------+-------+---------+-------+
| 5 | tom | 80.00 | wuxi | 2 |
+----+------+-------+---------+-------+
1 row in set (0.00 sec)
在tt表查询包含字符串zh与ng,且两个字符之间只有一个字符的name字段并打印出对应的记录
mysql> select * from tt where name regexp 'zh.ng';
+----+----------+-------+---------+-------+
| id | name | score | address | hobby |
+----+----------+-------+---------+-------+
| 3 | zhangsan | 80.00 | beijing | 2 |
+----+----------+-------+---------+-------+
1 row in set (0.00 sec)
mysql> select * from tt where name regexp 'sh.';
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
2 rows in set (0.00 sec)
mysql> select * from tt where name regexp 'zh.ng';
+----+----------+-------+---------+-------+
| id | name | score | address | hobby |
+----+----------+-------+---------+-------+
| 3 | zhangsan | 80.00 | beijing | 2 |
+----+----------+-------+---------+-------+
1 row in set (0.00 sec)`在这里插入代码片`
mysql> select * from tt where name regexp 'shu..';
+----+---------+-------+----------+-------+
| id | name | score | address | hobby |
+----+---------+-------+----------+-------+
| 8 | shuaige | 81.00 | hangzhou | 1 |
+----+---------+-------+----------+-------+
1 row in set (0.00 sec)
mysql> select * from tt where name regexp 'sh|i';
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 2 | lisi | 71.00 | nanjing | 0 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
3 rows in set (0.00 sec)
#这边我们首先先插入数据
mysql> insert into tt (name,score,address,hobby) values ('00',37,'tianjing',2),('0000',88,'shanghai',3);
#进行查询表数据
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 71.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 82.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
| 9 | 00 | 37.00 | tianjing | 2 |
| 10 | 0000 | 88.00 | shanghai | 3 |
+----+----------+-------+----------+-------+
10 rows in set (0.00 sec)
##匹配包含00或者更多0的字段
mysql> select * from tt where name regexp '00*';
+----+------+-------+----------+-------+
| id | name | score | address | hobby |
+----+------+-------+----------+-------+
| 9 | 00 | 37.00 | tianjing | 2 |
| 10 | 0000 | 88.00 | shanghai | 3 |
+----+------+-------+----------+-------+
2 rows in set (0.00 sec)
#*是否能匹配???
mysql> select * from tt where name regexp '*';
ERROR 1139 (42000): Got error 'repetition-operator operand invalid' from regexp
这边*不是显示所有 0*显示前面出现一个或者多个 *是次数的含义
mysql> select * from tt where name regexp '^[a-s]';
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 2 | lisi | 71.00 | nanjing | 0 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 6 | jerry | 82.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
+----+----------+-------+----------+-------+
5 rows in set (0.00 sec)
算数运算符
比较运算符
逻辑运算符
位运算符
运算符 | 描述 |
---|---|
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 取余数 |
例如,以 SELECT 命令来实现最基础的加减乘除运算,具体操作如下所示。
mysql> select 2+3,2*4,6%2,5/2;
+-----+-----+------+--------+
| 2+3 | 2*4 | 6%2 | 5/2 |
+-----+-----+------+--------+
| 5 | 8 | 0 | 2.5000 |
+-----+-----+------+--------+
1 row in set (0.00 sec)
在除法运算和求余数运算中,除数不能为 0,若除数是 0,返回的结果则为 NULL。需要注意的是,如果有多个运算符,按照先乘除后加减的优先级进行运算,相同优先级的运算 符没有先后顺序。
字符串的比较迷人不区分大小写,可使用binary来区分
运算符 | 描述 | 运算符 | 描述 |
---|---|---|---|
= | 等于 | IS NOT NULL | 判断一个值是否不为 NULL |
> | 大于 | BETWEEN AND | 两者之间 |
< | 小于 | IN | 在集合中 |
>= | 大于等于 | LIKE | 通配符匹配 |
<= | 小于等于 | GREATEST | 两个或多个参数时返回最大值 |
!=或<> | 不等于 | LEAST | 两个或多个参数时返回最小值 |
IS NULL | 判断一个值是否为 NULL | REGEXP | 正则表达式 |
等号(=)是用来判断数字、字符串和表达式是否相等的,如果相等则返回 1,如果不相等则返回 0。如果比较的两者有一个值是 NULL,则比较的结果就是 NULL。其中字符的比较是根据 ASCII 码来判断的,如果 ASCII 码相等,则表示两个字符相同;如果 ASCII 码不相等,则表示两个字符不相同。例如,等于运算符在数字、字符串和表达式上的使用,具 体操作如下所示。
mysql> select 2=3,3='3','q'='q',(3+4)=(2+5),'a'=NULL;
+-----+-------+---------+-------------+----------+
| 2=3 | 3='3' | 'q'='q' | (3+4)=(2+5) | 'a'=NULL |
+-----+-------+---------+-------------+----------+
| 0 | 1 | 1 | 1 | NULL |
+-----+-------+---------+-------------+----------+
1 row in set (0.00 sec)
从以上查询可以看出来:
如果两者都是整数,则按照整数值进行比较。
mysql> select 'pan'<>'chou',4!=5,NULL=NULL;
+---------------+------+-----------+
| 'pan'<>'chou' | 4!=5 | NULL=NULL |
+---------------+------+-----------+
| 1 | 1 | NULL |
+---------------+------+-----------+
1 row in set (0.00 sec)
#不等于运算符不能判断NULL
mysql> select 'hhh'=32;
+----------+
| 'hhh'=32 |
+----------+
| 0 |
+----------+
1 row in set, 1 warning (0.00 sec)
mysql> select 'hhh'!=32;
+-----------+
| 'hhh'!=32 |
+-----------+
| 1 |
+-----------+
1 row in set, 1 warning (0.00 sec)
具体内容点击 https://baike.baidu.com/item/ASCII/309296?fr=aladdin
我们需要大体记住几个常用Dec(十进制)的就行:
0是48
大写A是65 B是66 依次往后推算
小写a是97 b是98
mysql> select 5>3,'a'>'b',3>=4,(5+6)<=(3+2),5<3,3<2,'x'<='y',5<=5.5,'u'>=NULL;
+-----+---------+------+--------------+-----+-----+----------+--------+-----------+
| 5>3 | 'a'>'b' | 3>=4 | (5+6)<=(3+2) | 5<3 | 3<2 | 'x'<='y' | 5<=5.5 | 'u'>=NULL |
+-----+---------+------+--------------+-----+-----+----------+--------+-----------+
| 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | NULL |
+-----+---------+------+--------------+-----+-----+----------+--------+-----------+
1 row in set (0.00 sec)
mysql> select 2 is NULL,'d' is not NULL,NULL IS NULL;
+-----------+-----------------+--------------+
| 2 is NULL | 'd' is not NULL | NULL IS NULL |
+-----------+-----------------+--------------+
| 0 | 1 | 1 |
+-----------+-----------------+--------------+
1 row in set (0.00 sec)
mysql> select 5 between 2 and 8,7 between 5 and 10,'d' between 'a' and 'z';
+-------------------+--------------------+-------------------------+
| 5 between 2 and 8 | 7 between 5 and 10 | 'd' between 'a' and 'z' |
+-------------------+--------------------+-------------------------+
| 1 | 1 | 1 |
+-------------------+--------------------+-------------------------+
1 row in set (0.00 sec)
mysql> select 9 between 2 and 8,7 between 5 and 10,8 between 'a' and 'z';
+-------------------+--------------------+-----------------------+
| 9 between 2 and 8 | 7 between 5 and 10 | 8 between 'a' and 'z' |
+-------------------+--------------------+-----------------------+
| 0 | 1 | 0 |
+-------------------+--------------------+-----------------------+
1 row in set, 2 warnings (0.00 sec)
mysql> SELECT least(5,2,3),least('z','d','f'),greatest(1,2,6),greatest('a','d','c');
+--------------+--------------------+-----------------+-----------------------+
| least(5,2,3) | least('z','d','f') | greatest(1,2,6) | greatest('a','d','c') |
+--------------+--------------------+-----------------+-----------------------+
| 2 | d | 6 | d |
+--------------+--------------------+-----------------+-----------------------+
1 row in set (0.00 sec)
min是否能求最小值呢
mysql> select min(2,3,4),min('d','e','f');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3,4),min('d','e','f')' at line 1
#对了是不能的 min后面只能跟字段名称
mysql> select 2 in (1,2,3,4,5),'c' not in ('a','b','c');
+------------------+--------------------------+
| 2 in (1,2,3,4,5) | 'c' not in ('a','b','c') |
+------------------+--------------------------+
| 1 | 0 |
+------------------+--------------------------+
1 row in set (0.00 sec)
mysql> select 'fei' like 'shu_','like' like '%e','' not like '%a';
+-------------------+------------------+------------------+
| 'fei' like 'shu_' | 'like' like '%e' | '' not like '%a' |
+-------------------+------------------+------------------+
| 0 | 1 | 1 |
+-------------------+------------------+------------------+
1 row in set (0.00 sec)
运算符 | 描述 |
---|---|
NOT | 或 ! 逻辑非 |
AND 或 && | 逻辑与 |
OR 或 | |
XOR | 逻辑异或 |
逻辑运算符中最简单的运算符就是逻辑非,逻辑非使用 NOT 或!表示。逻辑非将跟在它后面的逻辑测试取反,把真变为假,把假变为真。如果 NOT 后面的操作数为 0 时,所得值为 1;如果操作数为非 0 时,所得值为 0;如果操作数为 NULL 时,所得值为 NULL。例如,对非 0 值和 0 值分别作逻辑非运算,具体操作如下所示。
mysql> select not 0,not 2,!6,!(3-3);
+-------+-------+----+--------+
| not 0 | not 2 | !6 | !(3-3) |
+-------+-------+----+--------+
| 1 | 0 | 0 | 1 |
+-------+-------+----+--------+
1 row in set (0.00 sec)
mysql> select 3 and 5,0 and 2,0 and NULL,3 and NULL;
+---------+---------+------------+------------+
| 3 and 5 | 0 and 2 | 0 and NULL | 3 and NULL |
+---------+---------+------------+------------+
| 1 | 0 | 0 | NULL |
+---------+---------+------------+------------+
1 row in set (0.00 sec)
一旦有个非0值,出来就是非0值
mysql> select 2 OR 5,0 OR 3,0 OR NULL,2 OR NULL;
+--------+--------+-----------+-----------+
| 2 OR 5 | 0 OR 3 | 0 OR NULL | 2 OR NULL |
+--------+--------+-----------+-----------+
| 1 | 1 | NULL | 1 |
+--------+--------+-----------+-----------+
1 row in set (0.00 sec)
mysql> SELECT 1 XOR 5,0 XOR 0,0 XOR 3,1 XOR NULL,NULL XOR NULL;
+---------+---------+---------+------------+---------------+
| 1 XOR 5 | 0 XOR 0 | 0 XOR 3 | 1 XOR NULL | NULL XOR NULL |
+---------+---------+---------+------------+---------------+
| 0 | 0 | 1 | NULL | NULL |
+---------+---------+---------+------------+---------------+
1 row in set (0.00 sec)
位运算符实际上是对二进制数进行计算的运算符。MySQL 内位运算会先将操作数变成二进制格式,然后进行位运算,最后在将计算结果从二进制变回到十进制格式,方便用户查看。
运算符 | 描述 |
---|---|
& | 按位与 |
l | |
~ | 按位取反 |
^ | 按位异或 |
<< | 按位左移 |
>> | 按位右移 |
示例:对数字进行按位与、或和取反运算
mysql> select 10&15,10|15,10^15,5 &~1;
+-------+-------+-------+-------+
| 10&15 | 10|15 | 10^15 | 5 &~1 |
+-------+-------+-------+-------+
| 10 | 15 | 5 | 4 |
+-------+-------+-------+-------+
1 row in set (0.00 sec)
mysql> select 1<<2;
+------+
| 1<<2 |
+------+
| 4 |
+------+
1 row in set (0.01 sec)
mysql> select 10<<2;
+-------+
| 10<<2 |
+-------+
| 40 |
+-------+
1 row in set (0.00 sec)
<<左移位运算 向左移动两位,空缺出补0
1<<2
0001
0100 左移后
10<<2
1010
101000 左移后
优先级 | 运算符 | 优优先级 | 运算符 |
---|---|---|---|
1 | ! 8 | l | |
2 | ~ | 9 | =,<=>,>=,>,<=,<,<>,!=,IS,LIKE,REGEXP,IN |
3 | ^ | 10 | BETWEEN,CASE,WHEN,THEN,ELSE |
4 | * | , /(DIV), %(MOD) | 11 |
5 | +,- | 12 | &&,AND |
6 | >>,<< | 13 | |
7 | & | 14 | := |
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 71.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 82.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
| 9 | 00 | 37.00 | tianjing | 2 |
| 10 | 0000 | 88.00 | shanghai | 3 |
+----+----------+-------+----------+-------+
10 rows in set (0.00 sec)
mysql> select * from gg;
+----+---------+
| id | gg_name |
+----+---------+
| 1 | 乔治 |
| 2 | 佩奇 |
| 3 | 苏西 |
+----+---------+
3 rows in set (0.01 sec)
上述操作是将 tt表的每一行与 gg表的每一行进行比较,以检查它们是否都满足条件。当满足条件时,INNER JOIN 将返回由 tt表和 gg表中的列组成的新行。如果没有匹配项,查询将返回一个空的结果集。
mysql> select T.name,G.gg_name from tt T left join gg G on T.hobby=G.id;
+----------+---------+
| name | gg_name |
+----------+---------+
| shanghai | 乔治 |
| jerry | 乔治 |
| shuaige | 乔治 |
| wangwu | 佩奇 |
| zhangsan | 佩奇 |
| tom | 佩奇 |
| aa | 佩奇 |
| 00 | 佩奇 |
| 0000 | 苏西 |
| lisi | NULL |
+----------+---------+
10 rows in set (0.00 sec)
#主表全部显示
从以上左连接查询结果可以看出,除了匹配的行,还包括了左表中有但右表中没有的行,如果有表没有对应值,则使用 NULL 代替。也就是说在左连接查询中,使用 NULL 值表示右表中没有找到与左表中匹配的记录。
右连接也被称为右外连接,在 FROM 子句中使用 RIGHT JOIN 或者 RIGHT OUTER JOIN 关键字来表示。右连接跟左连接正好相反,它是以右表为基础表,用于接收右表中的所有行,并用这些记录与左表中的行进行匹配。也就是说匹配右表中的每一行及左表中符合条件的记录。
mysql> select T.name,G.gg_name from tt T left join gg G on T.hobby=G.id;
+----------+---------+
| name | gg_name |
+----------+---------+
| shanghai | 乔治 |
| jerry | 乔治 |
| shuaige | 乔治 |
| wangwu | 佩奇 |
| zhangsan | 佩奇 |
| tom | 佩奇 |
| aa | 佩奇 |
| 00 | 佩奇 |
| 0000 | 苏西 |
| lisi | NULL |
+----------+---------+
10 rows in set (0.00 sec)
在右连接的查询结果集中,除了符合匹配规则的行外,还包括右表中有但是左表中不匹 配的行,这些记录在左表中以 NULL 补足。
MYSQL提供了实现各种功能的函数
mysql> select abs(-2),abs(-3.14);
+---------+------------+
| abs(-2) | abs(-3.14) |
+---------+------------+
| 2 | 3.14 |
+---------+------------+
1 row in set (0.00 sec)
mysql> select abs(2),abs(-3.14);
+--------+------------+
| abs(2) | abs(-3.14) |
+--------+------------+
| 2 | 3.14 |
+--------+------------+
1 row in set (0.00 sec)
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.6662262563279143 |
+--------------------+
1 row in set (0.00 sec)
#取值范围:0<=x<1
mysql> select mod(5,2);
+----------+
| mod(5,2) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
#3的3ci'fan
mysql> select power(3,3);
+------------+
| power(3,3) |
+------------+
| 27 |
+------------+
1 row in set (0.00 sec)
mysql> select power(4,3);
+------------+
| power(4,3) |
+------------+
| 64 |
+------------+
1 row in set (0.00 sec)
mysql> select round(2.49);
+-------------+
| round(2.49) |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
mysql> select round(3.67);
+-------------+
| round(3.67) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
mysql> select sqrt(4);
+---------+
| sqrt(4) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
mysql> select sqrt(9);
+---------+
| sqrt(9) |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
mysql> select truncate(3.1415926,2);
+-----------------------+
| truncate(3.1415926,2) |
+-----------------------+
| 3.14 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select ceil(3.4);
+-----------+
| ceil(3.4) |
+-----------+
| 4 |
+-----------+
1 row in set (0.00 sec)
mysql> select ceil(5.4);
+-----------+
| ceil(5.4) |
+-----------+
| 6 |
+-----------+
1 row in set (0.00 sec)
mysql> select greatest(11,12,9),least(10,7,14);
+-------------------+----------------+
| greatest(11,12,9) | least(10,7,14) |
+-------------------+----------------+
| 12 | 7 |
+-------------------+----------------+
1 row in set (0.00 sec)
length(x)返回字符串x的值
mysql> select length('aaaa');
+----------------+
| length('aaaa') |
+----------------+
| 4 |
+----------------+
1 row in set (0.00 sec)
mysql> select length('aa aa');
+-----------------+
| length('aa aa') |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.00 sec)
mysql> select trim('abcd');
+--------------+
| trim('abcd') |
+--------------+
| abcd |
+--------------+
1 row in set (0.00 sec)
mysql> select trim(' abcd');
+---------------+
| trim(' abcd') |
+---------------+
| abcd |
+---------------+
1 row in set (0.00 sec)
mysql> select trim('ab cd');
+---------------+
| trim('ab cd') |
+---------------+
| ab cd |
+---------------+
1 row in set (0.00 sec)
#只能去除前后两端空格 不能过滤中间的空格
mysql> select concat('abc','fff');
+---------------------+
| concat('abc','fff') |
+---------------------+
| abcfff |
+---------------------+
1 row in set (0.00 sec)
#空格是不能被过滤
mysql> select concat('abc',' fff');
+------------------------+
| concat('abc',' fff') |
+------------------------+
| abc fff |
+------------------------+
1 row in set (0.00 sec)
mysql> select concat('asd',trim(' qwe'));
+-----------------------------+
| concat('asd',trim(' qwe')) |
+-----------------------------+
| asdqwe |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select upper('cde'),lower('SDG');
+--------------+--------------+
| upper('cde') | lower('SDG') |
+--------------+--------------+
| CDE | sdg |
+--------------+--------------+
1 row in set (0.00 sec)
mysql> select left('certsd',3);
+------------------+
| left('certsd',3) |
+------------------+
| cer |
+------------------+
1 row in set (0.00 sec)
mysql> select right('certsd',3);
+-------------------+
| right('certsd',3) |
+-------------------+
| tsd |
+-------------------+
1 row in set (0.00 sec)
mysql> select concat(left('certsd',3),right('certsd',3));
+--------------------------------------------+
| concat(left('certsd',3),right('certsd',3)) |
+--------------------------------------------+
| certsd |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> select repeat('qwer',3);
+------------------+
| repeat('qwer',3) |
+------------------+
| qwerqwerqwer |
+------------------+
1 row in set (0.00 sec)
#做一个内嵌
mysql> select replace('dd','ua','xx');
+-------------------------+
| replace('dd','ua','xx') |
+-------------------------+
| dd |
+-------------------------+
1 row in set (0.01 sec)
mysql> select strcmp(14,14);
+---------------+
| strcmp(14,14) |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql> select strcmp(27,7);
+--------------+
| strcmp(27,7) |
+--------------+
| -1 |
+--------------+
1 row in set (0.00 sec)
mysql> select strcmp(7,17);
+--------------+
| strcmp(7,17) |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
#比较的是第一个数
mysql> select substring('defgdasd',2,3);
+---------------------------+
| substring('defgdasd',2,3) |
+---------------------------+
| efg |
+---------------------------+
1 row in set (0.00 sec)
#substring(完整字符串,起始位置,长度)
#倒过来输出
mysql> select reverse ('abc');
+-----------------+
| reverse ('abc') |
+-----------------+
| cba |
+-----------------+
1 row in set (0.00 sec)
#反过来大写
mysql> select upper(reverse ('abc'));
+------------------------+
| upper(reverse ('abc')) |
+------------------------+
| CBA |
+------------------------+
1 row in set (0.00 sec)```
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2020-11-02 |
+------------+
1 row in set (0.00 sec)
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 12:03:06 |
+-----------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2020-11-02 12:03:10 |
+---------------------+
1 row in set (0.00 sec)
mysql> select month('2020-08-25');
+---------------------+
| month('2020-08-25') |
+---------------------+
| 8 |
+---------------------+
1 row in set (0.00 sec)
mysql> select week('2020-08-25');
+--------------------+
| week('2020-08-25') |
+--------------------+
| 34 |
+--------------------+
1 row in set (0.00 sec)
mysql> select hour('16:35:53');
+------------------+
| hour('16:35:53') |
+------------------+
| 16 |
+------------------+
1 row in set (0.00 sec)
#显示当前的秒
mysql> select dayofweek(curdate());
+----------------------+
| dayofweek(curdate()) |
+----------------------+
| 2 |
+----------------------+
1 row in set (0.00 sec)
mysql> select dayofmonth(curdate());
+-----------------------+
| dayofmonth(curdate()) |
+-----------------------+
| 2 |
+-----------------------+
1 row in set (0.01 sec)
mysql> select dayofyear(curdate());
+----------------------+
| dayofyear(curdate()) |
+----------------------+
| 307 |
+----------------------+
1 row in set (0.00 sec)
存储过程的优点
执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率
SQL语句加上控制语句的集合,灵活性高
在服务器端存储,客户端调用时,降低网络负载
可多次重复被调用,可随时修改,不影响客户端调用
可完成所有的数据库操作,也可控制数据库的信息访问权限
CREATE PROCEDURE<过程名>([过程参数[…..]])<过程体>
[过程参数[.-..]]格式
IN| ouT|lNouTj参数名><类型>
参数分为
mysql>DELIMITER $$ //$$是用户自定义的结束符
//省略存储过程其他步骤
mysql> DELIMITER; //分号前有空格
#定义存储过程
delimiter $$
create procedure存储过程名(in 参数名参数类型)
begin
#定义变量
declare变量名变量类型
#变量赋值
set变量名=值
sql语句1;
sql语句2;
…
sql语句n;
end$
delimiter;
#调用存储过程
call存储过程名(实际参数);
#删除存储过程
drop procedure存储过程名;
mysql> delimiter $$
mysql> create procedure TT()
-> begin
-> select name,score from tt;
-> end$$
mysql> delimiter ; #将语句的结束符恢复为分号
mysql> show procedure status where db='tt'\G;
*************************** 1. row ***************************
Db: tt
Name: TT
Type: PROCEDURE
Definer: root@localhost
Modified: 2020-11-02 16:44:07
Created: 2020-11-02 16:44:07
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)
#不做为where条件筛选会输出的东西比较多
mysql> call TT();
+----------+-------+
| name | score |
+----------+-------+
| wangwu | 80.00 |
| lisi | 71.00 |
| zhangsan | 80.00 |
| shanghai | 83.00 |
| tom | 80.00 |
| jerry | 82.00 |
| aa | 87.00 |
| shuaige | 81.00 |
| 00 | 37.00 |
| 0000 | 88.00 |
+----------+-------+
10 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MySQL存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT,形式如:
CREATEPROCEDURE 存储过程名([[IN |OUT |INOUT ] 参数名 数据类形...])
mysql> create procedure PP(in my_name varchar(10))
-> begin
-> select name,score from tt where name=my_name;
-> end$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> show procedure status where db='tt';
+----+------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+----+------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| tt | PP | PROCEDURE | root@localhost | 2020-11-02 16:55:50 | 2020-11-02 16:55:50 | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| tt | TT | PROCEDURE | root@localhost | 2020-11-02 16:44:07 | 2020-11-02 16:44:07 | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
+----+------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
2 rows in set (0.00 sec)
#实现调用
mysql> call PP('lisi');
+------+-------+
| name | score |
+------+-------+
| lisi | 71.00 |
+------+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
加入控制语句
需求:输入一个成绩
把lisi进行分组 输入76分以上被分配到第一组 76以下改为第二组
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 71.00 | nanjing | 0 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 82.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
| 9 | 00 | 37.00 | tianjing | 2 |
+----+----------+-------+----------+-------+
9 rows in set (0.00 sec)
mysql> call CC(76);
Query OK, 1 row affected (0.01 sec)
mysql> select * from tt;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | wangwu | 80.00 | beijing | 2 |
| 2 | lisi | 76.00 | nanjing | 1 |
| 3 | zhangsan | 80.00 | beijing | 2 |
| 4 | shanghai | 83.00 | hangzhou | 1 |
| 5 | tom | 80.00 | wuxi | 2 |
| 6 | jerry | 82.00 | hangzhou | 1 |
| 7 | aa | 87.00 | shanghai | 2 |
| 8 | shuaige | 81.00 | hangzhou | 1 |
| 9 | 00 | 37.00 | tianjing | 2 |
+----+----------+-------+----------+-------+
9 rows in set (0.00 sec)
#已改好
有任何问题都可以在评论区留言或私聊博主。