对 MySQL 数据库的查询,除了基本的查询外,有时候需要对查询的结果集进行处理。 例如只取 10 条数据、对查询结果进行排序或分组等等
PS:类比于windows 任务管理器
使用 SELECT 语句可以将需要的数据从 MySQL 数据库中查询出来,如果对查询的结果进行排序,可以使用 ORDER BY 语句来对语句实现排序,并最终将排序后的结果返回给用户。这个语句的排序不光可以针对某一个字段,也可以针对多个字段
(1)语法:
SELECT column1, column2, … FROM table_name ORDER BY column1, column2, …
ASC|DESC;
ASC 是按照升序进行排序的,是默认的排序方式,即 ASC 可以省略。SELECT 语句中如果没有指定具体的排序方式,则默认按 ASC方式进行排序。
DESC 是按降序方式进 行排列。当然 ORDER BY 前面也可以使用 WHERE 子句对查询结果进一步过滤。
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,'chanyeol',80,'beijing',2);
insert into info values(2,'baekhyun',90,'shengzheng',2);
insert into info values(3,'billkin',60,'shanghai',4);
insert into info values(4,'pp',99,'hangzhou',5);
insert into info values(5,'sehun',98,'laowo',3);
insert into info values(6,'lay',10,'nanjing',3);
insert into info values(7,'suho',11,'nanjing',5);
mysql> select * from info;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 6 | lay | 10.00 | nanjing | 3 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
| 7 | suho | 11.00 | nanjing | 5 |
+------+----------+-------+------------+--------+
7 rows in set (0.00 sec)
select id,name,score from info order by score;
+------+----------+-------+
| id | name | score |
+------+----------+-------+
| 6 | lay | 10.00 |
| 7 | suho | 11.00 |
| 3 | billkin | 60.00 |
| 1 | chanyeol | 80.00 |
| 2 | baekhyun | 90.00 |
| 5 | sehun | 98.00 |
| 4 | pp | 99.00 |
+------+----------+-------+
7 rows in set (0.00 sec)
select id,name,score from info order by score desc;
+------+----------+-------+
| id | name | score |
+------+----------+-------+
| 4 | pp | 99.00 |
| 5 | sehun | 98.00 |
| 2 | baekhyun | 90.00 |
| 1 | chanyeol | 80.00 |
| 3 | billkin | 60.00 |
| 7 | suho | 11.00 |
| 6 | lay | 10.00 |
+------+----------+-------+
7 rows in set (0.00 sec)
select name,score from info where address='nanjing' order by score desc;
+------+-------+
| name | score |
+------+-------+
| suho | 11.00 |
| lay | 10.00 |
+------+-------+
2 rows in set (0.00 sec)
(2)ORDER BY 语句也可以使用多个字段来进行排序,当排序的第一个字段相同的记录有多条的情况下,这些多条的记录再按照第二个字段进行排序,ORDER BY 后面跟多个字段时,字段之间使用英文逗号隔开,优先级是按先后顺序而定,但order by 之后的第一个参数只有在出现相同值时,第二个字段才有意义
① 查询学生信息先按兴趣id降序排列,相同分数的,id也按降序排列:
select id,name,hobbid from info order by hobbid desc,id desc;
+------+----------+--------+
| id | name | hobbid |
+------+----------+--------+
| 7 | suho | 5 |
| 4 | pp | 5 |
| 3 | billkin | 4 |
| 6 | lay | 3 |
| 5 | sehun | 3 |
| 2 | baekhyun | 2 |
| 1 | chanyeol | 2 |
+------+----------+--------+
7 rows in set (0.00 sec)
② 查询学生信息先按兴趣id降序排列,相同分数的,id按升序排列:
select id,name,hobbid from info order by hobbid desc,id;
+------+----------+--------+
| id | name | hobbid |
+------+----------+--------+
| 4 | pp | 5 |
| 7 | suho | 5 |
| 3 | billkin | 4 |
| 5 | sehun | 3 |
| 6 | lay | 3 |
| 1 | chanyeol | 2 |
| 2 | baekhyun | 2 |
+------+----------+--------+
7 rows in set (0.00 sec)
(1)AND/OR ——且/或:
select * from info where score >70 and score <=90;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 80.00 | beijing | 2 |
+------+----------+-------+------------+--------+
(2)嵌套/多条件
select * from info where score >70 or (score >75 and score <90);
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
+------+----------+-------+------------+--------+
(3) distinct 查询不重复记录
① 语法:select distinct 字段 from 表名﹔
select distinct hobbid from info;
+--------+
| hobbid |
+--------+
| 2 |
| 4 |
| 3 |
| 5 |
+--------+
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理。
(1)语法:SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator value GROUP BY column_name;
① 按hobbid相同的分组,计算相同分数的学生个数(基于name个数进行计数):
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.01 sec)
② 结合where语句,筛选分数大于等于80的分组,计算学生个数:
select count(name),hobbid from info where score>=80 group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
| 2 | 2 |
| 1 | 3 |
| 1 | 5 |
+-------------+--------+
③ 结合order by把计算出的学生个数按升序排列:
select count(name),score,hobbid from info where score>=80 group by hobbid order by count(name) asc;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
| 1 | 99.00 | 5 |
| 1 | 98.00 | 3 |
| 2 | 90.00 | 2 |
+-------------+-------+--------+
3 rows in set (0.00 sec)
imit 限制输出的结果记录:在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录(行)。有时候仅 需要返回第一行或者前几行,这时候就需要用到 LIMIT 字句
(1)语法
SELECT column1, column2, … FROM table_name LIMIT [offset,] number
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。 如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的 位置偏移量是 0,第二条是 1,以此类推。第二个参数是设置返回记录行的最大数目。
① 查询所有信息显示前4行记录:
select * from info limit 3;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 80.00 | beijing | 2 |
+------+----------+-------+------------+--------+
3 rows in set (0.00 sec)
② 从第4行开始,往后显示3行内容:
select * from info limit 3,3;
+------+-------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-------+-------+----------+--------+
| 6 | lay | 10.00 | nanjing | 3 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
+------+-------+-------+----------+--------+
3 rows in set (0.00 sec)
④ 结合order by语句,按id的大小升序排列显示前三行:
select id,name from info order by id limit 3;
+------+----------+
| id | name |
+------+----------+
| 1 | chanyeol |
| 2 | baekhyun |
| 3 | billkin |
+------+----------+
3 rows in set (0.00 sec)
⑤ 基础select 小的升阶,输出最后三行:
select id,name from info order by id desc limit 3;
+------+-------+
| id | name |
+------+-------+
| 7 | suho |
| 6 | lay |
| 5 | sehun |
+------+-------+
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性
(1)语法
对于列的别名: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;
如果表的长度比较长,可以使用 AS 给表设置别名,在查询的过程中直接使用别名
临时设置info的别名为i
select i.name as 姓名,i.score as 成绩 from info as i;
① 查询info表的字段数量,以number显示
select count(*) as number from info;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
select count(*) number from info;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
(2)使用场景:
① 对复杂的表进行查询的时候,别名可以缩短查询语句的长度
② 多表相连查询的时候(通俗易懂、减短sql语句)
create table t1 as select * from info;
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
select * from t1;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 6 | lay | 10.00 | nanjing | 3 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
| 7 | suho | 11.00 | nanjing | 5 |
+------+----------+-------+------------+--------+
7 rows in set (0.00 sec)
此处AS起到的作用:
在为表设置别名时,要保证别名不能与数据库中的其他表的名称冲突。列的别名是在结果中有显示的,而表的别名在结果中没有显示,只在执行查询时使用。
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。
通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。常用的通配符有两个,分别是:
(1)需求:查询名字是c开头的记录:
select id,name from info where name like 'c%';
+------+----------+
| id | name |
+------+----------+
| 1 | chanyeol |
+------+----------+
(2)查询名字中间有a的记录
select id,name from info where name like '%a%';
+------+----------+
| id | name |
+------+----------+
| 2 | baekhyun |
| 1 | chanyeol |
| 6 | lay |
+------+----------+
3 rows in set (0.00 sec)
(3)查询bill后面3个字符的名字记录:
select id,name from info where name like 'bill___';
+------+---------+
| id | name |
+------+---------+
| 3 | billkin |
+------+---------+
1 row in set (0.00 sec)
(4)查询名字以b开头的记录:
select id,name from info where name like 'b%_';
+------+----------+
| id | name |
+------+----------+
| 2 | baekhyun |
| 3 | billkin |
+------+----------+
2 rows in set (0.00 sec)
(1)子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
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
PS:子语句中的sql语句是为了,最后过滤出一个结果集,用于主语句的判断条件
in: 将主表和子表关联/连接的语法
(2)多表查询:
create table test1(id int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test1 values(1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select id,name,score from info where id in (select * from test1);
+------+----------+-------+
| id | name | score |
+------+----------+-------+
| 2 | baekhyun | 90.00 |
| 3 | billkin | 60.00 |
| 1 | chanyeol | 80.00 |
+------+----------+-------+
3 rows in set (0.00 sec)
(3)语法:IN 用来判断某个值是否在给定的结果集中,通常结合子查询来使用
语法:<表达式> [NOT] IN <子查询>
① 查询分数大于80的记录:
select name,score from info where id in (select id from info where score>80);
+----------+-------+
| name | score |
+----------+-------+
| baekhyun | 90.00 |
| pp | 99.00 |
| sehun | 98.00 |
+----------+-------+
3 rows in set (0.00 sec)
② 将t1里的记录全部删除,重新插入info表的记录:
insert into t1 select * from info where id in (select id from info);
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 6 | lay | 10.00 | nanjing | 3 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
| 7 | suho | 11.00 | nanjing | 5 |
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 6 | lay | 10.00 | nanjing | 3 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
| 7 | suho | 11.00 | nanjing | 5 |
+------+----------+-------+------------+--------+
14 rows in set (0.00 sec)
(4)UPDATE 语句也可以使用子查询。UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
update info set score=60 where id in (select * from test1 where id=3);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from info;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 6 | lay | 10.00 | nanjing | 3 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
| 7 | suho | 11.00 | nanjing | 5 |
+------+----------+-------+------------+--------+
7 rows in set (0.00 sec)
update info set score=100 where id not in (select * from test1 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 |
+------+----------+--------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 3 | billkin | 60.00 | shanghai | 4 |
| 1 | chanyeol | 100.00 | beijing | 2 |
| 6 | lay | 100.00 | nanjing | 3 |
| 4 | pp | 100.00 | hangzhou | 5 |
| 5 | sehun | 100.00 | laowo | 3 |
| 7 | suho | 100.00 | nanjing | 5 |
+------+----------+--------+------------+--------+
7 rows in set (0.00 sec)
(5)DELETE 也适用于子查询:
delete from info where id in (select id where score>80);
Query OK, 6 rows affected (0.00 sec)
mysql> select * from info;
+------+---------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+---------+-------+----------+--------+
| 3 | billkin | 60.00 | shanghai | 4 |
+------+---------+-------+----------+--------+
delete from t1 where id not in (select id where score>=80);
Query OK, 6 rows affected (0.00 sec)
mysql> select id,name,score from t1;
+------+----------+-------+
| id | name | score |
+------+----------+-------+
| 2 | baekhyun | 90.00 |
| 1 | chanyeol | 80.00 |
| 4 | pp | 99.00 |
| 5 | sehun | 98.00 |
| 2 | baekhyun | 90.00 |
| 1 | chanyeol | 80.00 |
| 4 | pp | 99.00 |
| 5 | sehun | 98.00 |
+------+----------+-------+
8 rows in set (0.00 sec)
(6)EXISTS 在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
select count(*) from t1 where exists(select id from t1 where score=80);
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)
select count(*) from info where exists(select id from info where score<50);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射,视图可以理解为镜花水月/倒影,动态保存结果集(数据),基础表info (7行记录) ——》映射(投影)–视图。
针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示)
(1)select * from info; #展示的部分是info表
(2)select * from view_name; #展示的一张或多张表
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性,本质而言视图是一种select(结果集的呈现)
PS:视图适合于多表连接浏览时使用!不适合增、删、改,而存储过程适合于使用较频繁的SQL语句,这样可以提高执行效率!
(1)区别:
① 视图是已经编译好的sql语句。而表不是
② 视图没有实际的物理记录。而表有。show table status\G
③ 表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改
④ 视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
⑤ 表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
⑥ 视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的)
(2)联系:
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
(3)示例:
① 创建视图:
create view v1_score as select * from t1 where score>=90;
Query OK, 0 rows affected (0.01 sec)
mysql> show table status\G; #查看表的状态
select * from v1_score; #查看视图
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 4 | pp | 99.00 | hangzhou | 5 |
| 5 | sehun | 98.00 | laowo | 3 |
+------+----------+-------+------------+--------+
6 rows in set (0.00 sec)
desc v1_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)
② 多表创建视图:
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,'chanyeol',20);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test01 values(2,'baekhyun',30);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test01 values(3,'billkin',29);
Query OK, 1 row affected (0.01 sec)
create view v1_info(id,name,score,age) as select info.id,info.name,info.score,test01.age from info,test01 where info.name=test01.name;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from v1_info;
+------+---------+-------+------+
| id | name | score | age |
+------+---------+-------+------+
| 3 | billkin | 60.00 | 29 |
+------+---------+-------+------+
1 row in set (0.00 sec)
③ 修改原表数据:
update t1 set score='94' where name='sehun';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from v_score;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 5 | sehun | 94.00 | laowo | 3 |
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 80.00 | beijing | 2 |
| 5 | sehun | 94.00 | laowo | 3 |
+------+----------+-------+------------+--------+
6 rows in set (0.00 sec)
同时可以通过视图修改原表:
update v_score set score='99' where name='chanyeol';
select * from v_score;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 99.00 | beijing | 2 |
| 5 | sehun | 94.00 | laowo | 3 |
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 99.00 | beijing | 2 |
| 5 | sehun | 94.00 | laowo | 3 |
+------+----------+-------+------------+--------+
6 rows in set (0.00 sec)
select * from t1;
+------+----------+-------+------------+--------+
| id | name | score | address | hobbid |
+------+----------+-------+------------+--------+
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 99.00 | beijing | 2 |
| 4 | pp | 77.00 | hangzhou | 5 |
| 5 | sehun | 94.00 | laowo | 3 |
| 2 | baekhyun | 90.00 | shengzheng | 2 |
| 1 | chanyeol | 99.00 | beijing | 2 |
| 4 | pp | 77.00 | hangzhou | 5 |
| 5 | sehun | 94.00 | laowo | 3 |
+------+----------+-------+------------+--------+
8 rows in set (0.00 sec)
(4)总结:修改表不能修改以函数、复合函数方式计算出来的字段。
(1)在 SQL 语句使用过程中,经常会碰到 NULL 这几个字符。通常使用 NULL 来表示缺失 的值,也就是在表中该字段是没有值的。如果在创建表时,限制某些字段不为空,则可以使用 NOT NULL 关键字,不使用则默认可以为空。在向表内插入记录或者更新记录时,如果该字段没有 NOT NULL 并且没有值,这时候新记录的该字段将被保存为 NULL。需要注意 的是,NULL 值与数字 0 或者空白(spaces)的字段是不同的,值为 NULL 的字段是没有 值的。在 SQL 语句中,使用 IS NULL 可以判断表内的某个字段是不是 NULL 值,相反的用 IS NOT NULL 可以判断不是 NULL 值。
(2)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)
select * from info;
+------+---------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+---------+-------+----------+--------+
| 3 | billkin | 60.00 | shanghai | 4 |
+------+---------+-------+----------+--------+
1 row in set (0.00 sec)
mysql> insert into info values(1,'pp',null,'mangu',6);
Query OK, 1 row affected (0.00 sec)
mysql> mysql> select * from info;
+------+---------+-------+----------+--------+------+
| id | name | score | address | hobbid | addr |
+------+---------+-------+----------+--------+------+
| 3 | billkin | 60.00 | shanghai | 4 | chen |
| 2 | lanzhou | 90.00 | zhong | 7 | an |
| 1 | pp | NULL | mangu | 6 | NULL |
+------+---------+-------+----------+--------+------+
alter table info add column addr varchar(50); #在表中新增新的字段
#验证:
alter table info add column addr varchar(50);
select * from info;
+------+---------+-------+----------+--------+------+
| id | name | score | address | hobbid | addr |
+------+---------+-------+----------+--------+------+
| 3 | billkin | 60.00 | shanghai | 4 | chen |
| 1 | pp | NULL | mangu | 6 | NULL |
+------+---------+-------+----------+--------+------+
update info set addr='chen' where score >=60; #给表中分数超过60的数据新增地址为chen
统计数量:检测null是否会加入统计中:
select count(addr) from info;
+-------------+
| count(addr) |
+-------------+
| 2 |
+-------------+
select * from info where addr is NULL; #查询null值
+------+------+-------+---------+--------+------+
| id | name | score | address | hobbid | addr |
+------+------+-------+---------+--------+------+
| 1 | pp | NULL | mangu | 6 | NULL |
+------+------+-------+---------+--------+------+
1 row in set (0.00 sec)
select * from info where addr is not null; #查询不为空的值
+------+---------+-------+----------+--------+------+
| id | name | score | address | hobbid | addr |
+------+---------+-------+----------+--------+------+
| 3 | billkin | 60.00 | shanghai | 4 | chen |
| 2 | lanzhou | 90.00 | zhong | 7 | an |
+------+---------+-------+----------+--------+------+
2 rows in set (0.00 sec)
MySQL 的连接查询,通常都是将来自两个或多个表的记录行结合起来,基于这些表之间的 共同字段,进行数据的拼接。首先,要确定一个主表作为结果集,然后将其他表的行有选择 性的连接到选定的主表结果集上。使用较多的连接查询包括:内连接、左连接和右连接。
create table test2(a_id int(11) default null,a__name varchar(32) default null,a_level int(11) defaultt 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 来实现多表的内连接,不过为了更好的性能,建议最好不要超过三个表。
(1)语法
SELECT column_name(s)FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
(2)示例:
select info.id,info.name from info inner join infos on info.name=infos.name;
+------+----------+
| id | name |
+------+----------+
| 4 | chanyeol |
| 5 | baekhyun |
| 6 | sehun |
+------+----------+
(1)概念:左连接也可以被称为左外连接,在 FROM 子句中使用 LEFT JOIN 或者 LEFT OUTER JOIN 关键字来表示。左连接以左侧表为基础表,接收左表的所有行,并用这些行与右侧参 考表中的记录进行匹配,也就是说匹配左表中的所有行以及右表中符合条件的行。
(2)示例:
select * from info left join infos on info.name=infos.name;
+------+----------+-------+---------+----------+-------+----------+
| id | name | score | hobby | name | score | address |
+------+----------+-------+---------+----------+-------+----------+
| 4 | chanyeol | 72.50 | rap | chanyeol | 80.50 | beijing |
| 5 | baekhyun | 92.50 | singing | baekhyun | 99.50 | shanghai |
| 6 | sehun | 94.50 | dancing | sehun | 99.50 | nanjing |
| 1 | suho | 70.50 | running | NULL | NULL | NULL |
| 2 | chen | 92.50 | singing | NULL | NULL | NULL |
| 3 | xiumin | 94.50 | dancing | NULL | NULL | NULL |
+------+----------+-------+---------+----------+-------+----------+
6 rows in set (0.00 sec)
(1)概念:右连接也被称为右外连接,在 FROM 子句中使用 RIGHT JOIN 或者 RIGHT OUTER JOIN 关键字来表示。右连接跟左连接正好相反,它是以右表为基础表,用于接收右表中的所有行,并用这些记录与左表中的行进行匹配。
(2)示例:
select * from info right join infos on info.name=infos.name;
+------+----------+-------+---------+----------+-------+----------+
| id | name | score | hobby | name | score | address |
+------+----------+-------+---------+----------+-------+----------+
| 4 | chanyeol | 72.50 | rap | chanyeol | 80.50 | beijing |
| 5 | baekhyun | 92.50 | singing | baekhyun | 99.50 | shanghai |
| 6 | sehun | 94.50 | dancing | sehun | 99.50 | nanjing |
+------+----------+-------+---------+----------+-------+----------+
3 rows in set (0.00 sec)
在 SELECT 语句中嵌套一个 SELECT 子句,用来筛选出需要的数据。
(1)指定字段查询
(2)配合where条件语句查询。
(3)select配合limit输出指定多行内容。
(1)内连查询:inner join
(2)左连接:left join
(3)右连接:right join