文章目录
MySQL子查询,视图,链接查询
一、子查询
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语句。
子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
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: 将主表和子表关联/连接的语法
1. 不同表/多表示例
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)
2. 多表查询
使用表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 中也同样适用。
在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套。
2.1 语法
IN 用来判断某个值是否在给定的结果集中,通常结合子查询来使用
<表达式> [NOT] IN <子查询>
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。
若启用了 NOT 关键字,则返回值相反。
需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的
2.2 示例
(1)查询分数大于80的记录
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 from info where score>80;
+------+
| id |
+------+
| 5 |
| 4 |
| 2 |
+------+
3 rows in set (0.00 sec)
mysql> select name,score from info where id in (select id from info where score>80);
+----------+-------+
| name | score |
+----------+-------+
| jiaoshou | 98.00 |
| tianqi | 99.00 |
| wangwu | 90.00 |
+----------+-------+
3 rows in set (0.00 sec)
3.子查询 (INSERT 语句)
子查询还可以用在 INSERT 语句中。子查询的结果集可以通过 INSERT 语句插入到其 他的表中
注:创建时表必须已存在,并且表的格式要和复制的表的格式相同
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 | 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)
4.子查询 (UPDATE 语句)
UPDATE 语句也可以使用子查询。UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
mysql> select * from tree;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
#此处是查出tree中的id号对应将info中的id修改
mysql> update info set score=50 where id in (select * from tree where id=2);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
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 | 50.00 | shengzheng | 2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
4.1 子查询 (UPDATE 语句的not in)
表示 先匹配出tree表内的id字段为基础匹配的结果集(2,3)
然后再执行主语句,以主语句的id 为基础 进行where 条件判断/过滤
mysql> select * from tree;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> update info set score=100 where id not in (select * from tree where id >1));
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from info;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 6 | hanmeimei | 100.00 | nanjing | 3 |
| 5 | jiaoshou | 100.00 | laowo | 3 |
| 7 | lilei | 100.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 100.00 | beijing | 2 |
| 4 | tianqi | 100.00 | hangzhou | 5 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
5.子查询(DELETE语句)
*注:在删除之前一定要确定好删除的部分,再三确认
5.1 需求:删除分数大于80的记录
mysql> select * from info;
+------+-----------+--------+------------+--------+
| id | name | score | address | hobbid |
+------+-----------+--------+------------+--------+
| 6 | hanmeimei | 100.00 | nanjing | 3 |
| 5 | jiaoshou | 100.00 | laowo | 3 |
| 7 | lilei | 100.00 | nanjing | 5 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 1 | liuyi | 100.00 | beijing | 2 |
| 4 | tianqi | 100.00 | hangzhou | 5 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)
mysql> delete from info where id in (select id where score>80);
Query OK, 5 rows affected (0.00 sec)
mysql> select * from info;
+------+--------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+--------+-------+------------+--------+
| 3 | lisi | 60.00 | shanghai | 4 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
+------+--------+-------+------------+--------+
2 rows in set (0.00 sec)
5.2 IN 前面还可以添加 NOT
在 IN 前面还可以添加 NOT,其作用与IN相反,表示否定(即不在子查询的结果集里面)
5.2.1需求:删除分数不是大于等于80的记录
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 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
mysql> delete from t1 where id not in (select id where score>=80);
Query OK, 3 rows affected (0.00 sec)
mysql> select id,name,score from t1;
+------+----------+-------+
| id | name | score |
+------+----------+-------+
| 5 | jiaoshou | 98.00 |
| 1 | liuyi | 80.00 |
| 4 | tianqi | 99.00 |
| 2 | wangwu | 90.00 |
+------+----------+-------+
4 rows in set (0.00 sec)
6. 子查询 (EXISTS 语句)
EXISTS 这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
6.1 查询如果存在分数等于80的记录则计算test2的字段数
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 count(*) from tree where exists(select id from test2 where sccore=80);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
6.2 检验如果存在分数小于50的记录则计算test2的字段数
如果有的话输出所有数的字段数,没有则返回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 | 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)
#有小于50的值返回所有字段数
mysql> select count(*) from test2 where exists(select id from test2 where score<50);
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
#没有大于110的值返回0
mysql> select count(*) from test2 where exists(select id from test2 where score>110);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
7. 子查询(别名as)
#查询info表id,name 字段
select id,name from info;
以上命令可以查看到info表的内容
7.1别名的应用场景
将结果集做为一张表进行查询的时候,我们也需要用到别名
7.1.1 需求:从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 表.字段,字段 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)
二、MySQL视图
1.MySQL视图的概念
数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射
视图可以理解映射出来虚拟的,动态保存结果集(数据)
基础表——映射(投影)=视图
2.视图作用场景
针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示)
3.视图作用范围
select * from info; #展示的部分是info表
select * from view_name; #展示的一张或多张表
4.视图功能
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性
本质而言视图是一种select(结果集的呈现)
注:视图适合于多表连接浏览时使用!不适合增、删、改
而存储过程适合于使用较频繁的SQL语句,这样可以提高执行效率!
5. 视图和表的区别和联系
5.1 区别
①、视图是已经编译好的sql语句。而表不是
②、视图没有实际的物理记录。而表有。
show table status\G
③、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改
④、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
⑤、表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
⑥、视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的)
5.2 联系
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
6.示例
6.1 满足80分的学生展示在视图中
注:这个结果会动态变化,同时可以给不同的人群(例如权限范围)展示不同的视图
6.1.1 创建视图(单表)
create view v_score as select * from test2 where score>=80;
Query OK, 0 rows affected (0.00 sec)
6.1.2 查看表状态
show table status\G
6.1.3 查看视图
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)
6.1.4 查看视图与源表结构
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)
6.2 多表创建视图
创建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)
6.2.1 需求:需要创建一个视图,需要输出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)
6.3 修改原表数据
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)
6.4 同时可以通过视图修改原表
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——所拥有权限 ,视图无法显示完整的约束
7.NULL 值
在 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)
7.1 插入一条记录,分数字段输入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 | shengzheng | 2 | |
| 3 | zjf | 80.00 | shanghai | 4 | nj |
±-----±---------±-------±-----------±-------±-----+
4 rows in set (0.00 sec)
三、连接查询
MySQL 的连接查询,通常都是将来自两个或多个表的记录行结合起来,基于这些表之间的 共同字段,进行数据的拼接。首先,要确定一个主表作为结果集,然后将其他表的行有选择 性的连接到选定的主表结果集上。使用较多的连接查询包括:内连接、左连接和右连接
1.模板数据
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);
2.内连接
输出匹配共同数据/共同字段数据内容
MySQL 中的内连接就是两张或多张表中同时符合某种条件的数据记录的组合。通常在 FROM 子句中使用关键字 INNER JOIN 来连接多张表,并使用 ON 子句设置连接条件,内连接是系统默认的表连接,所以在 FROM 子句后可以省略 INNER 关键字,只使用 关键字 JOIN。同时有多个表时,也可以连续使用 INNER JOIN 来实现多表的内连接,不过为了更好的性能,建议最好不要超过三个表。
2.1 语法
SELECT column_name(s)FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
2.2 示例
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 的方式将两张表指定的相同字段的记录行输出出来
内连查询:面试,直接了当的说 用inner join 就可以
2.左连接
将两张表的内容进行匹配按照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。
3.右连接
将两张表的内容进行匹配按照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)
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。
## 3.右连接
将两张表的内容进行匹配按照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 补足