(增、删、改、查)
对 MySQL 数据库的查询,除了基本的查询外,有时候需要对查询的结果集进行处理。
例如只取 10 条数据、对查询结果进行排序或分组等等
类比于windows 任务管理器
使用 SELECT 语句可以将需要的数据从 MySQL 数据库中查询出来,如果对查询的结果进行排序,可以使用 ORDER BY 语句来对语句实现排序,并最终将排序后的结果返回给用户。这个语句的排序不光可以针对某一个字段,也可以针对多个字段
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ...
ASC 是按照升序进行排序的,默认的排序方式,即 ASC 可以省略。
DESC 是按降序方式进 行排列。当然 ORDER BY 前面也可以使用 WHERE 子句对查询结果进一步过滤。
准备: 数据库有一张info表,记录了学生的id,姓名,分数,地址和爱好
create database class;
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| class |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
use class;
create table info (id int,name varchar(10) primary key not null ,score decimal(5,2),address varchar(20),hobbid int(5));
insert into info values(1,'liuyi',80,'beijing',2);
insert into info values(2,'wangwu',90,'shengzheng',2);
insert into info values(3,'lisi',60,'shanghai',4);
insert into info values(4,'tianqi',99,'hangzhou',5);
insert into info values(5,'jiaoshou',98,'laowo',3);
insert into info values(6,'hanmeimei',10,'nanjing',3);
insert into info values(7,'lilei',11,'nanjing',5);
mysql> select * from info;
+------+-----------+-------+------------+--------+
| 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)
mysql> select id,name,score from info order by score;
+------+-----------+-------+
| id | name | score |
+------+-----------+-------+
| 6 | hanmeimei | 10.00 |
| 7 | lilei | 11.00 |
| 3 | lisi | 60.00 |
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 5 | jiaoshou | 98.00 |
| 4 | tianqi | 99.00 |
+------+-----------+-------+
7 rows in set (0.00 sec)
mysql> select id,name,score from info order by score desc;
+-----------+-------+
| name | score |
+-----------+-------+
| tangyan | 98.00 |
| shidapeng | 90.00 |
| chengu | 88.00 |
| shangzhen | 80.00 |
| zhaokun | 80.00 |
| xiawenjie | 80.00 |
| caicai | 70.00 |
+-----------+-------+
7 rows in set (0.01 sec)
mysql> select * from info;
+------+-----------+-------+------------+--------+
| 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)
mysql> select name,score from info where address='nanjing' order by score desc;
+-----------+-------+
| name | score |
+-----------+-------+
| lilei | 11.00 |
| hanmeimei | 10.00 |
+-----------+-------+
2 rows in set (0.00 sec)
ORDER BY 语句也可以使用多个字段来进行排序,当排序的第一个字段相同的记录有多条的情况下,这些多条的记录再按照第二个字段进行排序,ORDER BY 后面跟多个字段时,字段之间使用英文逗号隔开,优先级是按先后顺序而定
但order by 之后的第一个参数只有在出现相同值时,第二个字段才有意义
mysql> select id,name,hobbid from info 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 |
+------+-----------+--------+
7 rows in set (0.00 sec)
mysql> select id,name,hobbid from info 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 |
+------+-----------+--------+
7 rows in set (0.00 sec)
mysql> select * from info where score >70 and score <=90;
+------+--------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+--------+-------+------------+--------+
2 rows in set (0.00 sec)
mysql> select * from info where score >70 or score <=90;
+------+-----------+-------+------------+--------+
| 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)
mysql> select * from info where score >70 or (score >75 and score <90);
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)
mysql> select * from info where score >70 or (score >75 and score <90) order by id;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)
语法:
select distinct 字段 from 表名﹔
示例:
mysql> select distinct hobbid from info;
+--------+
| hobbid |
+--------+
| 2 |
| 4 |
| 5 |
| 3 |
+--------+
通过 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 valueGROUP BY column_name;
mysql> select count(name),hobbid from info group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 2 | 3 |
| 1 | 4 |
| 2 | 5 |
+-------------+--------+
4 rows in set (0.00 sec)
mysql> select count(name),hobbid from info where score >=80 group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 1 | 3 |
| 1 | 5 |
+-------------+--------+
3 rows in set (0.00 sec)
mysql> select count(name),score,hobbid from info where score >=80 group by hobbid order by count(name) asc;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
| 1 | 98.00 | 3 |
| 1 | 99.00 | 5 |
| 2 | 80.00 | 2 |
+-------------+-------+--------+
3 rows in set (0.00 sec)
limit 限制输出的结果记录
在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录(行)。
有时候仅 需要返回第一行或者前几行,这时候就需要用到limit子句
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。
如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的 位置偏移量是 0,第二条是
1,以此类推。第二个参数是设置返回记录行的最大数目。
mysql> select * from info limit 4;
+------+-----------+-------+----------+--------+
| 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 |
+------+-----------+-------+----------+--------+
4 rows in set (0.00 sec)
mysql> select * from info limit 4,2;
+------+--------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+----------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
+------+--------+-------+----------+--------+
2 rows in set (0.00 sec)
mysql> select id,name from info order by id limit 3;
+------+--------+
| id | name |
+------+--------+
| 1 | liuyi |
| 2 | wangwu |
| 3 | lisi |
+------+--------+
3 rows in set (0.00 sec)
mysql> select id,name from info order by id desc limit 3;
+------+-----------+
| id | name |
+------+-----------+
| 7 | lilei |
| 6 | hanmeimei |
| 5 | jiaoshou |
+------+-----------+
3 rows in set (0.01 sec)
在 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
之后的别名,主要是为表内的列或者表提供临时的名称,在查询过程中使用,库内实际的表名 或字段名是不会被改变的
select name as 姓名,score as 成绩 from info;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| hanmeimei | 10.00 |
| jiaoshou | 98.00 |
| lilei | 11.00 |
| lisi | 60.00 |
| liuyi | 80.00 |
| tianqi | 99.00 |
| wangwu | 90.00 |
+-----------+--------+
7 rows in set (0.00 sec)
如果表的长度比较长,可以使用 AS 给表设置别名,在查询的过程中直接使用别名,临时设置info的别名为i
select i.name as 姓名,i.score as 成绩 from info as i;
查询info表的字段数量,以number显示
mysql> select count(*) as number from info;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
不用as也可以,一样显示
mysql> select count(*) number from info;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
创建t1表,将info表的查询记录全部插入t1表
mysql> create table t1 as select * from info;
mysql> select * from t1;
+------+-----------+-------+------------+--------+
| 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 |
+------+-----------+-------+------------+--------+
注:此处AS起到的作用:
创建了一个新表t1 并定义表结构,插入表数据(与info表相同)
但是”约束“没有被完全”复制“过来 #但是如果原表设置了主键,那么附表的:default字段会默认设置一个0
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。 通常通配符都是跟 LIKE 一起使用的,并协同 WHERE
子句共同来完成查询任务。常用的通配符有两个,分别是:
%:百分号表示零个、一个或多个字符 *
_:下划线表示单个字符
mysql> select id,name from info where name like 'l%';
+------+-------+
| id | name |
+------+-------+
| 7 | lilei |
| 3 | lisi |
| 1 | liuyi |
+------+-------+
3 rows in set (0.00 sec)
mysql> select id,name from info where name like 'l_s_';
+------+------+
| id | name |
+------+------+
| 3 | lisi |
+------+------+
1 row in set (0.00 sec)
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语句。
子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。 PS:
子语句可以与主语句所查询的表相同,也可以是不相同
:
select name,score from info where id in (select id from info where score >80);
以上同表示例:
主语句:select name,score from info where id
子语句(集合): select id from info where score >80
注:子语句中的sql语句是为了,最后过滤出一个结果集,用于主语句的判断条件 in: 将主表和子表关联/连接的语法
mysql> create table info(id int);
ERROR 1050 (42S01): Table 'info' already exists
mysql> create table tree(id int);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into tree values(1),(2),(3);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from tree;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
使用表info和tree相同的id查询出来
mysql> select id,name,score from info where id in (select * from tree);
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 3 | lisi | 60.00 |
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
3 rows in set (0.01 sec)
mysql> select * from info;
+------+-----------+-------+------------+--------+
| 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)
子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE
中也同样适用。在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套。
#IN 用来判断某个值是否在给定的结果集中,通常结合子查询来使用
<表达式> [NOT] IN <子查询>
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。 若启用了 NOT
关键字,则返回值相反。需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。
多数情况下,子查询都是与 SELECT 语句一起使用的
查询分数大于80的记录
mysql> select name,score from info where id in (select id from info where score>80);
+----------+-------+
| name | score |
+----------+-------+
| wangwu | 90.00 |
| tianqi | 99.00 |
| jiaoshou | 98.00 |
| lilei | 11.00 |
+----------+-------+
4 rows in set (0.01 sec)
子查询还可以用在 INSERT 语句中。子查询的结果集可以通过 INSERT 语句插入到其 他的表中
将t1里的记录全部删除,重新插入info表的记录
mysql> insert into t1 select * from info where id in (select id from info);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+-----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+----------+-------+
| 1 | shidapeng | 90.00 | nanjing | NULL |
| 2 | shangzhen | 80.00 | beijing | NULL |
| 3 | tangyan | 98.00 | shanghai | NULL |
| 6 | chengu | 88.00 | nanjing | NULL |
| 7 | caicai | 70.00 | hangzhou | NULL |
| 8 | zhaokun | 80.00 | hangzhou | NULL |
| 9 | xiawenjie | 80.00 | hangzhou | NULL |
+----+-----------+-------+----------+-------+
7 rows in set (0.00 sec)
UPDATE 语句也可以使用子查询。UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
将caicai的分数改为50
mysql> update info set score=50 where id in (select * from ky11 where id=2);
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from info;
+------+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 5 | lilei | 11.00 | nanjing | 5 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
update info set score=100 where id not in (select * from member where id >1);
表示 先匹配出member表内的id字段为基础匹配的结果集(2,3)
然后再执行主语句,以主语句的id 为基础 进行where 条件判断/过滤
DELETE 也适用于子查询
删除分数大于80的记录
mysql> delete from info where id in (select id where score>80);
Query OK, 3 rows affected (0.00 sec)
mysql> select id,name,score from t1;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 2 | shangzhen | 80.00 |
| 7 | caicai | 50.00 |
| 8 | zhaokun | 80.00 |
| 9 | xiawenjie | 80.00 |
+----+-----------+-------+
4 rows in set (0.00 sec)
在 IN 前面还可以添加 NOT,其作用与IN相反,表示否定(即不在子查询的结果集里面)
删除分数不是大于等于80的记录
mysql> delete from t1 where id not in (select id where score>=80);
Query OK, 1 row affected (0.00 sec)
mysql> select id,name,score from t1;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 2 | shangzhen | 80.00 |
| 8 | zhaokun | 80.00 |
| 9 | xiawenjie | 80.00 |
+----+-----------+-------+
3 rows in set (0.00 sec)
EXISTS 这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
查询如果存在分数等于80的记录则计算info的字段数
mysql> select count(*) from info where exists(select id from info where score=80);
#学校里面 (人员信息统计,只有当所有人全部签到之后,在人员信息统计表录入完成侯,我才需要进行统计)
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
查询如果存在分数小于50的记录则计算info的字段数,info表没有小于50的,所以返回0
mysql> select count(*) from info where exists(select id from info where score<50);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
#查询info表id,name 字段
select id,name from info;
以上命令可以查看到info表的内容
#将结果集做为一张表进行查询的时候,我们也需要用到别名,
示例:
需求:从info表中的id和name字段的内容做为"内容" 输出id的部分
mysql> select id from (select id,name from info);
ERROR 1248 (42000): Every derived table must have its own alias
#此时会报错,原因为:
select * from 表名 此为标准格式,而以上的查询语句,“表名"的位置其实是一个完整结果集,mysql并不能直接识别,而此时给与结果集设置一个别名,以”select a.id from a“的方式查询将此结果集视为一张"表”,就可以正常查询数据了,如下:
select a.id from (select id,name from info) a;
相当于
select info.id,name from info;
select 表.字段,字段 from 表;
mysql> select id from (select id,name from info);
ERROR 1248 (42000): Every derived table must have its own alias
mysql> select a.id from (select id,name from info) a;
+------+
| id |
+------+
| 3 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> select info.id,name from info;
+------+--------+
| id | name |
+------+--------+
| 3 | lisi |
| 2 | wangwu |
+------+--------+
2 rows in set (0.00 sec)
数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射
视图可以理解为镜花水月/倒影,动态保存结果集(数据)
基础表→映射(投影)=视图
针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示)
作用范围:
select * from info; #展示的部分是info表
select * from view_name; #展示的一张或多张表
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性
本质而言视图是一种select(结果集的呈现)
PS:视图适合于多表连接浏览时使用!不适合增、删、改
而存储过程适合于使用较频繁的SQL语句,这样可以提高执行效率!
区别:
①、视图是已经编译好的sql语句。而表不是
②、视图没有实际的物理记录。而表有。
show table status\G
③、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改
④、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
⑤、表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
⑥、视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的)
联系:
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
满足80分的学生展示在视图中
注:这个结果会动态变化,同时可以给不同的人群(例如权限范围)展示不同的视图
create view v_score as select * from test2 where score>=80;
Query OK, 0 rows affected (0.00 sec)
查看表状态
show table status\G
mysql> select * from v_score;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)
查看视图与源表结构
mysql> desc v_score;
+---------+--------------+------+-----+---------+-------+
| 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)
mysql> desc test2;
+---------+--------------+------+-----+---------+-------+
| 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)
查看视图与源表结构
mysql> desc v_score;
+---------+--------------+------+-----+---------+-------+
| 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)
mysql> desc test2;
+---------+--------------+------+-----+---------+-------+
| 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)
多表创建视图
创建test03表
create table test03 (id int,name varchar(10),age char(10));
insert into test03 values(1,'zhangsan',20);
insert into test03 values(2,'lisi',30);
insert into test03 values(3,'wangwu',29);
mysql> select * from test03;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
+------+----------+------+
3 rows in set (0.00 sec)
创建一个视图,需要输出id、学生姓名、分数
mysql> select * from test03;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
+------+----------+------+
3 rows in set (0.00 sec)
create view v_test2(id,name,score) as select test2.id,test2.name,test2.score from test2,test03 where test2.name=test03.name;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from v_test2;
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 3 | lisi | 60.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
2 rows in set (0.00 sec)
修改原表数据
mysql> select * from test2;
+------+-----------+-------+------------+--------+
| 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)
mysql> select * from v_test2;
+------+--------+-------+
| id | name | score |
+------+--------+-------+
| 3 | lisi | 60.00 |
| 2 | wangwu | 90.00 |
+------+--------+-------+
2 rows in set (0.00 sec)
修改原表
update test2 set score='100' where name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
原表与视图变化
mysql> select * from test2;
+------+-----------+--------+------------+--------+
| 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 | 100.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)
mysql> select * from v_test2;
+------+--------+--------+
| id | name | score |
+------+--------+--------+
| 3 | lisi | 100.00 |
| 2 | wangwu | 90.00 |
+------+--------+--------+
2 rows in set (0.00 sec)
同时可以通过视图修改原表
update v_info set score='120' where name='wangwu';
mysql> select * from v_info;
+------+--------+--------+
| id | name | score |
+------+--------+--------+
| 3 | lisi | 80.00 |
| 2 | wangwu | 120.00 |
+------+--------+--------+
2 rows in set (0.00 sec)
mysql> select * from info;
+------+-----------+--------+------------+--------+
| 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 | 60.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 2 | wangwu | 120.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
修改表不能修改以函数、复合函数方式计算出来的字段 作用:查询方便、安全性
查询方便:索引速度快、同时可以多表查询更为迅速(视图不保存真实数据,视图本质类似select)
安全性:我们实现登陆的账户是root——所拥有权限 ,视图无法显示完整的约束
在 SQL 语句使用过程中,经常会碰到 NULL 这几个字符。 通常使用 NULL 来表示缺失 的值,也就是在表中该字段是没有值的。
如果在创建表时,限制某些字段不为空,则可以使用 NOT NULL 关键字,不使用则默认可以为空。
在向表内插入记录或者更新记录时,如果该字段没有 NOT NULL 并且没有值,这时候新记录的该字段将被保存为 NULL。
需要注意 的是,NULL 值与数字 0 或者空白(spaces)的字段是不同的,值为 NULL 的字段是没有 值的。在 SQL 语句中,使用
IS NULL 可以判断表内的某个字段是不是 NULL 值,相反的用 IS NOT NULL 可以判断不是 NULL 值。
查询info表结构,name字段是不允许空值的 null值与空值的区别(空气与真空)
空值长度为0,不占空间,NULL值的长度为null,占用空间 is null无法判断空值 空值使用"=“或者”<>"来处理(!=)
count()计算时,NULL会忽略,空值会加入计算
mysql> desc info;
+---------+--------------+------+-----+---------+-------+
| 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)
插入一条记录,分数字段输入null,显示出来就是null
alter table info add column addr varchar(50);
update info set addr='nj' where score >=70;
统计数量:检测null是否会加入统计中
mysql> select count(addr) from info;
+-------------+
| count(addr) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
将info表中其中一条数据修改为空值’’
mysql> update info set addr='' where name='wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from info;
+------+-----------+--------+------------+--------+------+
| id | name | score | address | hobbid | addr |
+------+-----------+--------+------------+--------+------+
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL |
| 5 | jiaoshou | 98.00 | laowo | 3 | nj |
| 7 | lilei | 11.00 | nanjing | 5 | NULL |
| 1 | liuyi | 60.00 | beijing | 2 | NULL |
| 4 | tianqi | 99.00 | hangzhou | 5 | nj |
| 2 | wangwu | 120.00 | shengzheng | 2 | |
| 3 | zjf | 80.00 | shanghai | 4 | nj |
+------+-----------+--------+------------+--------+------+
7 rows in set (0.00 sec)
统计数量,检测空值是不会被添加到统计中
mysql> select count(addr) from info;
+-------------+
| count(addr) |
+-------------+
| 4 |
+-------------+
1 row in set (0.00 sec)
查询null值
mysql> select * from info where addr is NULL;
+------+-----------+-------+---------+--------+------+
| id | name | score | address | hobbid | addr |
+------+-----------+-------+---------+--------+------+
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL |
| 7 | lilei | 11.00 | nanjing | 5 | NULL |
| 1 | liuyi | 60.00 | beijing | 2 | NULL |
+------+-----------+-------+---------+--------+------+
3 rows in set (0.00 sec)
查询不为空的值
mysql> select * from info where addr is not null;
+------+-----------+-------+---------+--------+------+
| id | name | score | address | hobbid | addr |
+------+-----------+-------+---------+--------+------+
| 5 | jiaoshou | 98.00 | laowo | 3 | nj |
| 4 | tianqi | 99.00 | hangzhou| 5 | nj |
| 2 | wangwu | 120.00| shengzhe| 2 | nj |
| 3 | zjf | 80.00 | shanghai| 4 | nj |
+------+-----------+-------+---------+--------+------+
4 rows in set (0.00 sec)
MySQL 的连接查询,通常都是将来自两个或多个表的记录行结合起来,基于这些表之间的 共同字段,进行数据的拼接。首先,要确定一个主表作为结果集,然后将其他表的行有选择 性的连接到选定的主表结果集上。使用较多的连接查询包括:内连接、左连接和右连接
create table test1 (
a_id int(11) default null,
a_name varchar(32) default null,
a_level int(11) default null);
create table test2 (
b_id int(11) default null,
b_name varchar(32) default null,
b_level int(11) default null);
insert into test1 values (1,'aaaa',10);
insert into test1 values (2,'bbbb',20);
insert into test1 values (3,'cccc',30);
insert into test1 values (4,'dddd',40);
insert into test2 values (2,'bbbb',20);
insert into test2 values (3,'cccc',30);
insert into test2 values (5,'eeee',50);
insert into test2 values (6,'ffff',60);
输出匹配共同数据/共同字段数据内容
MySQL 中的内连接就是两张或多张表中同时符合某种条件的数据记录的组合。通常在 FROM 子句中使用关键字 INNER JOIN
来连接多张表,并使用 ON 子句设置连接条件,内连接是系统默认的表连接,所以在 FROM 子句后可以省略 INNER 关键字,只使用 关键字
JOIN。同时有多个表时,也可以连续使用 INNER JOIN 来实现多表的内连接,不过为了更好的性能,建议最好不要超过三个表。
SELECT column_name(s)FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
insert into infos values('wangwu',80,'beijing'),('zhangsan',99,'shanghai'),('lisi',90,'nanjing'););
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from infos;
+----------+-------+----------+
| name | score | address |
+----------+-------+----------+
| wangwu | 80.00 | beijing |
| zhangsan | 99.00 | shanghai |
| lisi | 90.00 | nanjing |
+----------+-------+----------+
3 rows in set (0.00 sec)
mysql> select info.id,info.name from info inner join infos on info.name=infos.name;
+------+--------+
| id | name |
+------+--------+
| 2 | wangwu |
+------+--------+
1 row in set (0.00 sec)
内连查询:通过inner join 的方式将两张表指定的相同字段的记录行输出出来
将两张表的内容进行匹配按照sql查询的顺序(左——右)输出左表的全部内容和右表的共同数据的内容(以左表为主)
左连接也可以被称为左外连接,在 FROM 子句中使用 LEFT JOIN 或者 LEFT OUTER JOIN 关键字来表示。
mysql> select * from info left join infos on info.name=infos.name;
+------+-----------+--------+------------+--------+------+--------+-------+---------+
| id | name | score | address | hobbid | addr | name | score | address |
+------+-----------+--------+------------+--------+------+--------+-------+---------+
| 2 | wangwu | 120.00 | shengzheng | 2 | | wangwu | 80.00 | beijing |
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL | NULL | NULL | NULL |
| 5 | jiaoshou | 98.00 | laowo | 3 | nj | NULL | NULL | NULL |
| 7 | lilei | 11.00 | nanjing | 5 | NULL | NULL | NULL | NULL |
| 1 | liuyi | 60.00 | beijing | 2 | NULL | NULL | NULL | NULL |
| 4 | tianqi | 99.00 | hangzhou | 5 | nj | NULL | NULL | NULL |
| 3 | zjf | 80.00 | shanghai | 4 | nj | NULL | NULL | NULL |
+------+-----------+--------+------------+--------+------+--------+-------+---------+
7 rows in set (0.00 sec)
左连接中左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录,右表记录不足的地方均为 NULL。
将两张表的内容进行匹配按照sql查询的顺序(右——左)输出右表的全部内容和左表的共同数据的内容(以右表为主)
右连接也被称为右外连接,在 FROM 子句中使用 RIGHT JOIN 或者 RIGHT OUTER JOIN 关键字来表示。
mysql> select * from info right join infos on info.name=infos.name;
+------+--------+--------+------------+--------+------+----------+-------+----------+
| id | name | score | address | hobbid | addr | name | score | address |
+------+--------+--------+------------+--------+------+----------+-------+----------+
| 2 | wangwu | 120.00 | shengzheng | 2 | | wangwu | 80.00 | beijing |
| NULL | NULL | NULL | NULL | NULL | NULL | zhangsan | 99.00 | shanghai |
| NULL | NULL | NULL | NULL | NULL | NULL | lisi | 90.00 | nanjing |
+------+--------+--------+------------+--------+------+----------+-------+----------+
3 rows in set (0.00 sec)
在右连接的查询结果集中,除了符合匹配规则的行外,还包括右表中有但是左表中不匹 配的行,这些记录在左表中以 NULL。