首先创建例表,并添加数据
创建一张test表,记录了学生的id,姓名,分数,地址和爱好
create table test (id int,name varchar(10) primary key not null ,score decimal(5,2),address varchar(20),hobbid int(5));
insert into test values(1,'liuyi',80,'beijing',2);
insert into test values(2,'wangwu',90,'shengzheng',2);
insert into test values(3,'lisi',60,'shanghai',4);
insert into test values(4,'tianqi',99,'hangzhou',5);
insert into test values(5,'jiaoshou',98,'laowo',3);
insert into test values(6,'hanmeimei',10,'nanjing',3);
insert into test values(7,'lilei',11,'nanjing',5);
对查询的结果进行排序,可以使用 ORDER BY 语句来对语句实现排序,并最终将排序后的结果返回给用户。这个语句的排序不光可以针对某一个字段,也可以针对多个字段
ASC 是按照升序进行排序的,是默认的排序方式,即 ASC 可以省略。SELECT 语句中如果没有指定具体的排序方式,则默认按 ASC方式进行排序。
DESC 是按降序方式进 行排列。当然 ORDER BY 前面也可以使用 WHERE 子句对查询结果进一步过滤。
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ...
查看表内容
mysql> select * from test;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
mysql> select * from test order by score [asc 默认,可不加];
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+-----------+-------+------------+--------+
mysql> select * from test order by score desc;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
+------+-----------+-------+------------+--------+
例如:升序排列分数,要求爱好id为2
mysql> select * from test where hobbid=2 order by score;
+------+--------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+--------+-------+------------+--------+
当排序的第一个字段相同的记录有多条的情况下,这些多条的记录再按照第二个字段进行排序,ORDER BY 后面跟多个字段时,字段之间使用英文逗号隔开,优先级是按先后顺序而定
但order by 之后的第一个参数只有在出现相同值时,第二个字段才有意义
例如:按兴趣id降序排列,相同兴趣的,id也按降序排列
mysql> select id,name,hobbid from test order by hobbid desc,id desc;
+------+-----------+--------+
| id | name | hobbid |
+------+-----------+--------+
| 7 | lilei | 5 |
| 4 | tianqi | 5 |
| 3 | lisi | 4 |
| 6 | hanmeimei | 3 |
| 5 | jiaoshou | 3 |
| 2 | wangwu | 2 |
| 1 | liuyi | 2 |
+------+-----------+--------+
例如:按兴趣id降序排列,相同兴趣的,id按升序排列
mysql> select id,name,hobbid from test order by hobbid desc,id;
+------+-----------+--------+
| id | name | hobbid |
+------+-----------+--------+
| 4 | tianqi | 5 |
| 7 | lilei | 5 |
| 3 | lisi | 4 |
| 5 | jiaoshou | 3 |
| 6 | hanmeimei | 3 |
| 1 | liuyi | 2 |
| 2 | wangwu | 2 |
+------+-----------+--------+
#查询分数在60到90之间的数据
mysql> select * from test where score<90 and score>=60;
+------+-------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-------+-------+----------+--------+
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
+------+-------+-------+----------+--------+
#查询分数小于60的、分数大于90的
mysql> select * from test where score>90 or score<60;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+-----------+-------+----------+--------+
mysql> select * from test where score>90 or (score<=60 and address='shanghai');
+------+----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+----------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+----------+-------+----------+--------+
查询不重复记录
mysql> select distinct hobbid from test;
+--------+
| hobbid |
+--------+
| 3 |
| 5 |
| 4 |
| 2 |
+--------+
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理。
SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator value GROUP BY column_name;
例如:统计爱好相同的人数
mysql> select count(name),hobbid from test group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 2 | 3 |
| 1 | 4 |
| 2 | 5 |
+-------------+--------+
例如:筛选出成绩大于80的,统计爱好个数
mysql> select count(name),hobbid from test where score>=80 group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 1 | 3 |
| 1 | 5 |
+-------------+--------+
例如:筛选成绩大于80的,并统计爱好个数,按人数升序排列
mysql> select count(name),hobbid from test where score>=80 group by hobbid order by count(name);
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 1 | 3 |
| 1 | 5 |
| 2 | 2 |
+-------------+--------+
限制输出的结果记录
在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录(行)。有时候仅 需要返回第一行或者前几行,这时候就需要用到 LIMIT 子句
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。 如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的 位置偏移量是 0,第二条是 1,以此类推。第二个参数是设置返回记录行的最大数目。
#查询前4行的内容
mysql> select * from test order by id limit 4;
+------+--------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+--------+-------+------------+--------+
#查询第三行后的三行内容
mysql> select * from test order by id limit 3,3;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
+------+-----------+-------+----------+--------+
设置别名
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性
对于列的别名:SELECT column_name AS alias_name FROM table_name;
对于表的别名:SELECT column_name(s) FROM table_name AS alias_name;
在使用 AS 后,可以用 alias_name 代替 table_name,其中 AS 语句是可选的。AS 之后的别名,主要是为表内的列或者表提供临时的名称,在查询过程中使用,库内实际的表名 或字段名是不会被改变的
mysql> select name as 姓名,score as 成绩 from test;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| hanmeimei | 10.00 |
| jiaoshou | 98.00 |
| lilei | 11.00 |
| lisi | 60.00 |
| liuyi | 80.00 |
| tianqi | 99.00 |
| wangwu | 90.00 |
+-----------+--------+
如果表的长度比较长,可以使用 AS 给表设置别名,在查询的过程中直接使用别名
临时设置test的别名为i
select i.name as 姓名,i.score as 成绩 from test as i;
mysql> select name as 姓名,score 成绩,address 城市 from test;
+-----------+--------+------------+
| 姓名 | 成绩 | 城市 |
+-----------+--------+------------+
| hanmeimei | 10.00 | nanjing |
| jiaoshou | 98.00 | laowo |
| lilei | 11.00 | nanjing |
| lisi | 60.00 | shanghai |
| liuyi | 80.00 | beijing |
| tianqi | 99.00 | hangzhou |
| wangwu | 90.00 | shengzheng |
+-----------+--------+------------+
1、对复杂的表进行查询的时候,别名可以缩短查询语句的长度
2、多表相连查询的时候(通俗易懂、减短sql语句)
例如:创建新表test01时,将test表的查询记录全部插入test01表
mysql> create table test01 as select * from test;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from test01;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
1、创建了一个新表test01 并定义表结构,插入表数据(与test表相同)
2、但是”约束“没有被完全”复制“过来 #但是如果原表设置了主键,那么附表的:default字段会默认设置一个0
mysql> desc test;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> desc test01;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
在为表设置别名时,要保证别名不能与数据库中的其他表的名称冲突。
列的别名是在结果中有显示的,而表的别名在结果中没有显示,只在执行查询时使用。
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。
通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。
常用的通配符有两个,分别是:
%:百分号表示匹配零个、一个或多个字符 ;
_:下划线表示匹配单个字符 .
#查询姓名以l开头的数据
mysql> select * from test where name like 'l%';
+------+-------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-------+-------+----------+--------+
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
+------+-------+-------+----------+--------+
#查询姓名第二个字母为a的数据
mysql> select * from test where name like '_a%';
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
注: 子语句可以与主语句所查询的表相同,也可以是不同表
#相同表查询
mysql> select id,name,score from test where id in (select id from test where id<=2);
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
#不同表查询
mysql> select id,name,score from test where score in (select score from test01 where id>3);
+------+-----------+-------+
| id | name | score |
+------+-----------+-------+
| 6 | hanmeimei | 10.00 |
| 5 | jiaoshou | 98.00 |
| 7 | lilei | 11.00 |
| 4 | tianqi | 99.00 |
+------+-----------+-------+
<表达式> [NOT] IN <子查询>
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。 若启用了 NOT 关键字,则返回值相反。需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的
子查询的结果集可以通过 INSERT 语句插入到其 他的表中
#创建表结构和test相同的表tes02
mysql> create table test02 like test;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from test02;
Empty set (0.00 sec)
#使用子查询插入数据到test02
mysql> insert into test02 select * from test where id >3;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from test02;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+-----------+-------+----------+--------+
UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
mysql> update test02 set score=66 where id in (select id from test where id>=6);
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from test02;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | hanmeimei | 66.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 66.00 | nanjing | 5 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+-----------+-------+----------+--------+
mysql> delete from test02 where id in (select id from test where score>90);
Query OK, 2 rows affected (0.01 sec)
mysql> select * from test02;
+------+-----------+-------+---------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+---------+--------+
| 6 | hanmeimei | 66.00 | nanjing | 3 |
| 7 | lilei | 66.00 | nanjing | 5 |
+------+-----------+-------+---------+--------+
这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
#例如:当查询出test01表中有成绩大于90的时候,统计表test中的条目数
mysql> select count(*) from test where exists(select * from test01 where score>90);
+----------+
| count(*) |
+----------+
| 7 |
+----------+
#当查询出表test01中有id=9的数据时 统计test表中的条目数
mysql> select count(*) from test where exists(select * from test01 where id=9);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
视图:优化操作+安全方案
数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射
视图可以理解为镜花水月/倒影,动态保存结果集(数据)
作用场景[图]:
针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示)
作用范围:
select * from test; #展示的部分是info表
select * from view_name; #展示的一张或多张表
视图的功能:
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性
本质而言视图是一种select(结果集的呈现)
PS:视图适合于多表连接浏览时使用!不适合增、删、改
而存储过程适合于使用较频繁的SQL语句,这样可以提高执行效率!
视图是已经编译好的sql语句。而表不是 |
视图没有实际的物理记录。而表有。(show table status\G) |
表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改 |
视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。 |
表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表 |
视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的) |
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
#例如:创建视图,要求展示满足分数大于80的数据
mysql> create view score_test as select * from test where score>80;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from score_test;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
mysql> desc test;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | PRI | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> desc score_test;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| hobbid | int(5) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
#创建一张新的表
mysql> create table test01 (id int,name varchar(10),age char(10));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test01 values(1,'zhangsan',20);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test01 values(2,'lisi',30);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test01 values(3,'wangwu',29);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test01;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
+------+----------+------+
#多表创建
例如:需要创建一个视图,需要输出id、学生姓名、分数以及年龄
mysql> create view age_test as select t.id,t.name,t.score,t01.age from test t,test01 t01 where t.name=t01.name;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from age_test;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 80.00 | 30 |
| 2 | wangwu | 90.00 | 29 |
+------+--------+-------+------+
mysql> select * from age_test;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 80.00 | 30 |
| 2 | wangwu | 90.00 | 29 |
+------+--------+-------+------+
2 rows in set (0.00 sec)
mysql> update test01 set age=18 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from age_test;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 80.00 | 18 |
| 2 | wangwu | 90.00 | 29 |
+------+--------+-------+------+
mysql> update score_test set name='wuyanzu' where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from score_test;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wuyanzu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
mysql> select * from test;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 80.00 | shanghai | 4 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wuyanzu | 90.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+