目录
编辑
一、子查询
1.1语法
1.2select
1.3insert
1.3update
1.4delete
1.5 exists
1.6别名as
二、MySQL视图
2.1功能
2.2区别
2.3联系
2.4 创建视图(单表)
2.5 创建视图(多表)
2.6修改原表数据
2.7修改视图数据
三、NULL值
一、子查询
子查询也被称作内查询或者嵌套查询
是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE 中也同样适用。在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套。
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
1.1语法
语法:
<表达式> [NOT] IN <子查询>
<表达式> IN <子查询>
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。
若启用了 NOT 关键字,则返回值相反。
需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的
1.2select
#查询分数大于80的记录
mysql> select name,score from info3 where id in (select id info3 where score>80);
+-----------+-------+
| name | score |
+-----------+-------+
| jiangshou | 88.00 |
| lisi | 83.00 |
| tianqi | 92.00 |
| wangwu | 99.00 |
+-----------+-------+
4 rows in set (0.00 sec)
1.3insert
子查询的结果集可以通过 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)
1.3update
UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
mysql> update info3 set score=100 where id in (select * from info4 where id>1);
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from info3;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 5 | hanmei | 100.00 | nanjing | 3 |
| 7 | jixiaoman | 100.00 | guizhou | 5 |
| 6 | lilei | 100.00 | kunshan | 3 |
| 3 | lisi | 100.00 | shandong | 4 |
| 1 | luiyi | 100.00 | beijing | 2 |
| 4 | tangsan | 100.00 | laowo | 5 |
| 2 | wangwu | 100.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
mysql> update info3 set score=101 where id not in (select * fromm info4 where id>1);
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from info3;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 5 | hanmei | 101.00 | nanjing | 3 |
| 7 | jixiaoman | 101.00 | guizhou | 5 |
| 6 | lilei | 101.00 | kunshan | 3 |
| 3 | lisi | 100.00 | shandong | 4 |
| 1 | luiyi | 101.00 | beijing | 2 |
| 4 | tangsan | 101.00 | laowo | 5 |
| 2 | wangwu | 100.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
1.4delete
在 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)
1.5 exists
EXISTS 这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE。
#查询如果存在分数等于80的记录则计算info的字段数
mysql> select * from info3 where exists(select id from info3 where ere score=80);
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | husan | 23.00 | nanjing | 3 |
| 5 | jiangshou | 88.00 | laowo | 3 |
| 3 | lisi | 60.00 | shenzhen | 4 |
| 7 | liulei | 18.00 | nanjing | 5 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 92.00 | hangzhou | 5 |
| 2 | wangwu | 99.00 | shanghai | 2 |
+------+-----------+-------+----------+--------+
7 rows in set (0.00 sec)
mysql> select count(*) from info3 where exists (select id from innfo3 where score=80);
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
1.6别名as
#将结果集做为一张表进行查询的时候,我们也需要用到别名,示例: 需求:从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“的方式查询将此结果集视为一张"表",就可以正常查询数据了,
mysql> select a.id from (select id,name from info3) a;
+------+
| id |
+------+
| 6 |
| 5 |
| 3 |
| 7 |
| 1 |
| 4 |
| 2 |
+------+
7 rows in set (0.00 sec)
二、MySQL视图
2.1功能
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性 本质而言视图是一种select(结果集的呈现)
2.2区别
①、视图是已经编译好的sql语句。而表不是
②、视图没有实际的物理记录。而表有。 show table status\G
③、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改
④、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
⑤、表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
⑥、视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的
2.3联系
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
2.4 创建视图(单表)
mysql> create view v_sore as select * from info3 where score>=80;
Query OK, 0 rows affected (0.00 sec)
mysql> desc v_sore;
+---------+--------------+------+-----+---------+-------+
| 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.01 sec)
mysql> desc info3;
+---------+--------------+------+-----+---------+-------+
| 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> select * from v_sore;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 5 | jiangshou | 88.00 | laowo | 3 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 92.00 | hangzhou | 5 |
| 2 | wangwu | 99.00 | shanghai | 2 |
+------+-----------+-------+----------+--------+
4 rows in set (0.00 sec)
mysql> show table status\G
.
.
.
.
.
*************************** 8. row ***************************
Name: v_sore
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW
8 rows in set (0.01 sec)
2.5 创建视图(多表)
需求:需要创建一个视图,需要输出id、学生姓名、分数以及年龄
mysql> select * from test01;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 28 |
+------+----------+------+
3 rows in set (0.00 sec)
mysql> select * from info3;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | husan | 70.00 | nanjing | 3 |
| 5 | jiangshou | 88.00 | laowo | 3 |
| 3 | lisi | 83.00 | shenzhen | 4 |
| 7 | liulei | 18.00 | nanjing | 5 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 92.00 | hangzhou | 5 |
| 2 | wangwu | 99.00 | shanghai | 2 |
+------+-----------+-------+----------+--------+
7 rows in set (0.00 sec)
mysql> create view v_info3(id,name,score,age) as select a.id,a.name,a.score,b.age from info3 a,test01 b where a.name=b.name;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from v_info3;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 60.00 | 30 |
| 2 | wangwu | 99.00 | 28 |
+------+--------+-------+------+
2 rows in set (0.00 sec)
2.6修改原表数据
修改原表,视图会发送变化。
mysql> select * from v_info3;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 60.00 | 30 |
| 2 | wangwu | 99.00 | 28 |
+------+--------+-------+------+
2 rows in set (0.00 sec)
mysql> update info3 set score=83 where name='lisi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from v_info3;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 83.00 | 30 |
| 2 | wangwu | 99.00 | 28 |
+------+--------+-------+------+
2 rows in set (0.00 sec)
2.7修改视图数据
修改视图,原表也会发生随之改变。
#修改视图之前的表
mysql> select * from info3;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | husan | 70.00 | nanjing | 3 |
| 5 | jiangshou | 88.00 | laowo | 3 |
| 3 | lisi | 83.00 | shenzhen | 4 |
| 7 | liulei | 18.00 | nanjing | 5 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 92.00 | hangzhou | 5 |
| 2 | wangwu | 99.00 | shanghai | 2 |
+------+-----------+-------+----------+--------+
7 rows in set (0.00 sec)
#修改视图之前的视图
mysql> select * from v_info3;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 83.00 | 30 |
| 2 | wangwu | 99.00 | 28 |
+------+--------+-------+------+
2 rows in set (0.00 sec)
#将wangwu的score改成38
mysql> pumysql> update v_info3 set score=38 where name='wangwu';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#修改之后的视图
mysql> select * from v_info3;
+------+--------+-------+------+
| id | name | score | age |
+------+--------+-------+------+
| 3 | lisi | 83.00 | 30 |
| 2 | wangwu | 38.00 | 28 |
+------+--------+-------+------+
2 rows in set (0.00 sec)
#修改之后的表
mysql> select * from info3;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | husan | 70.00 | nanjing | 3 |
| 5 | jiangshou | 88.00 | laowo | 3 |
| 3 | lisi | 83.00 | shenzhen | 4 |
| 7 | liulei | 18.00 | nanjing | 5 |
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 92.00 | hangzhou | 5 |
| 2 | wangwu | 38.00 | shanghai | 2 |
+------+-----------+-------+----------+--------+
7 rows in set (0.00 sec)
三、NULL值
在创建表时,限制某些字段不为空,则可以使用 NOT NULL 关键字,不使用则默认可以为空。在向表内插入记录或者更新记录时,如果该字段没有 NOT NULL 并且没有值,这时候新记录的该字段将被保存为 NULL。
NULL 值与数字 0 或者空白(spaces)的字段是不同的,值为 NULL 的字段是没有 值的。
mysql> select length(null),length(''),length(123);
+--------------+------------+-------------+
| length(null) | length('') | length(123) |
+--------------+------------+-------------+
| NULL | 0 | 3 |
+--------------+------------+-------------+
1 row in set (0.00 sec)