(增、删、改、查)
对 MySQL 数据库的查询,除了基本的查询外,有时候需要对查询的结果集进行处理。 例如只取 10 条数据、对查询结果进行排序或分组等等
配置环境:数据库有一张info表,记录了学生的id,姓名,分数,地址和爱好
mysql> select * from cy ;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
| 6 | hanmeimei | 39.00 | nanjing | 3 |
| 7 | lilei | 22.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
# 语法格式
SELECT column1,... FROM 表名 ORDER BY column1, column2, ...
ASC|DESC; ASC 是按照升序进行排序的,是默认的排序方式,即 ASC 可以省略。
DESC 是按降序方式进行排列。当然 ORDER BY 前面也可以使用WHERE 子句对查询结果进一步过滤。
#按分数排序,默认不指定是升序排列 asc
mysql> select id,name,score from cy order by score;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 8 | caicai | 16.00 |
| 7 | lilei | 22.00 |
| 6 | hanmeimei | 39.00 |
| 3 | lisi | 60.00 |
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 5 | jiaoshou | 93.00 |
| 4 | tianqi | 95.00 |
+----+-----------+-------+
8 rows in set (0.00 sec)
#分数按降序排列,使用DESC
mysql> select id,name,score from cy order by score desc;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 4 | tianqi | 95.00 |
| 5 | jiaoshou | 93.00 |
| 2 | wangwu | 90.00 |
| 1 | liuyi | 80.00 |
| 3 | lisi | 60.00 |
| 6 | hanmeimei | 39.00 |
| 7 | lilei | 22.00 |
| 8 | caicai | 16.00 |
+----+-----------+-------+
8 rows in set (0.00 sec)
#order by还可以结合where进行条件过滤,筛选地址是南京的学生按分数降序排列
mysql> select name,score from cy where address='nanjing' order by score desc;
+-----------+-------+
| name | score |
+-----------+-------+
| hanmeimei | 39.00 |
| lilei | 22.00 |
| caicai | 16.00 |
+-----------+-------+
3 rows in set (0.01 sec)
order by之后的参数,使用","分割,优先级是按先后顺序而定,例如:
mysql> select id,name,hobby from cy order by hobby desc,id asc;
+----+-----------+-------+
| id | name | hobby |
+----+-----------+-------+
| 4 | tianqi | 5 |
| 7 | lilei | 5 |
| 8 | caicai | 5 |
| 3 | lisi | 4 |
| 5 | jiaoshou | 3 |
| 6 | hanmeimei | 3 |
| 1 | liuyi | 2 |
| 2 | wangwu | 2 |
+----+-----------+-------+
8 rows in set (0.00 sec)
小结: order by 之后的第一个参数只有在出现相同的数值,第二个字段才有意义
(示例: select id, hobby from info order by id asc, hobby desc; )
#AND/OR ——且/或
mysql> select * from cy where score >60 and score <=90;
#满足分数大于60而已小于等于90.
mysql> select * from cy where score >60 and score <=90;
+----+--------+-------+------------+-------+
| id | name | score | address | hobby |
+----+--------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+----+--------+-------+------------+-------+
2 rows in set (0.00 sec)
mysql> select * from cy where score >50 or score <=90;
#满足分数小于等于90或大于50
mysql> select * from cy ;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
| 6 | hanmeimei | 39.00 | nanjing | 3 |
| 7 | lilei | 22.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
#满足其中一个条件即可所以会全部显示
#嵌套/多条件
mysql> select * from cy where score >70 or (score >80 and score <90);
+----+----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
+----+----------+-------+------------+-------+
4 rows in set (0.00 sec)
#满足分数大于70或者分数在75到90之间
语法:
select distinct 字段 from 表名﹔
select distinct hobby from cy;
查询cy表中的兴趣列,不重复
mysql> select distinct hobby from cy;
+-------+
| hobby |
+-------+
| 2 |
| 4 |
| 5 |
| 3 |
+-------+
4 rows in set (0.00 sec)
1.distinct必须放在最开头
2.distinct只能使用需要去重的字段进行操作。
(也就是说我sidtinct了name , hobby两个字段,我后面想根据id进行排序,是不可以的,因为只能name,age两个字段进行操作)
3.distinct去重多个字段时,含义是:几个字段同时重复时才会被过滤。
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,
常用的聚合函数包括:计数(COUNT)、求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理。
函数 | 说明 |
---|---|
avg() | 返回指定列的平均值 |
count() | 返回指定列中非 NULL 值的个数 |
min() | 返回指定列的最小值 |
max() | 返回指定列的最大值 |
sum(x) | 返回指定列的所有值之和 |
(1)语法
selet字段,聚合函数 from 表名,(where字段名(匹配)数值) group by 字段名;
示例:
mysql> select count(name),score from cy where score>=50 group by score order by count(score) des
c;+-------------+-------+
| count(name) | score |
+-------------+-------+
| 1 | 93.00 |
| 1 | 80.00 |
| 1 | 90.00 |
| 1 | 60.00 |
| 1 | 95.00 |
+-------------+-------+
5 rows in set (0.00 sec)
#count(name):对名字进行计数
对cy进行分组,筛选范围/条件是score大于等于50的 'name',score相同的会默认分在一个组
结合order by把统计的id数量进行按降序序排列
#分组条件
结合where语句,筛选分数大于等于80的分组,计算学生个数按降序排列
mysql> select count(name),score,hobby from cy where score>=80 group by hobby order by count(score) desc;
+-------------+-------+-------+
| count(name) | score | hobby |
+-------------+-------+-------+
| 2 | 80.00 | 2 |
| 1 | 93.00 | 3 |
| 1 | 95.00 | 5 |
+-------------+-------+-------+
3 rows in set (0.00 sec)
limit 限制输出的结果记录
(1)语法
SELECT 字段 FROM 表名 LIMIT [offset,] number
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。
如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的 位置偏移量是 0,第二条是
1,以此类推。第二个参数是设置返回记录行的最大数目。
#查询所有信息显示前5行记录
mysql> select * from cy limit 4;
+----+--------+-------+------------+-------+
| id | name | score | address | hobby |
+----+--------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
+----+--------+-------+------------+-------+
4 rows in set (0.00 sec)
#从第2行开始,往后显示3行内容
mysql> select * from cy limit 1,3;
+----+--------+-------+------------+-------+
| id | name | score | address | hobby |
+----+--------+-------+------------+-------+
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
+----+--------+-------+------------+-------+
3 rows in set (0.00 sec)
#结合order by语句,按score的大小升序排列显示前三行
mysql> select id,name,score from cy order by score limit 3;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 8 | caicai | 16.00 |
| 7 | lilei | 22.00 |
| 6 | hanmeimei | 39.00 |
+----+-----------+-------+
3 rows in set (0.00 sec)
#基础select 小的升阶 怎么输出最后三行
mysql> select id,name,score from cy order by score desc limit 3;
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 4 | tianqi | 95.00 |
| 5 | jiaoshou | 93.00 |
| 2 | wangwu | 90.00 |
+----+----------+-------+
3 rows in set (0.00 sec)
输出前三行,怎么输出 : limit 3 limit 2 您说的是前三行,limit
是做为位置偏移量的定义,他的起始是从0开始,而0表示的是字段
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者
多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性
语法
对于列的别名:SELECT 字段 AS 别名 FROM 表名;
对于表的别名:SELECT 字段(s) FROM 表名 AS 别名;
在使用 AS 后,可以用 alias_name 代替 table_name,其中 AS 语句是可选的。AS
之后的别名,主要是为表内的列或者表提供临时的名称,在查询过程中使用,库内实际的表名或字段名是不会被改变的
#列别名设置示例:
mysql> select name as 姓名,score as 成绩 from cy;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| liuyi | 80.00 |
| wangwu | 90.00 |
| lisi | 60.00 |
| tianqi | 95.00 |
| jiaoshou | 93.00 |
| hanmeimei | 39.00 |
| lilei | 22.00 |
| caicai | 16.00 |
+-----------+--------+
8 rows in set (0.00 sec)
#如果表的长度比较长,可以使用 AS 给表设置别名,在查询的过程中直接使用别名
临时设置cy的别名为a
mysql> select a.name as 姓名,a.score as 成绩 from cy as a ;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| liuyi | 80.00 |
| wangwu | 90.00 |
| lisi | 60.00 |
| tianqi | 95.00 |
| jiaoshou | 93.00 |
| hanmeimei | 39.00 |
| lilei | 22.00 |
| caicai | 16.00 |
+-----------+--------+
8 rows in set (0.00 sec)
#查询info表的字段数量,以number显示
mysql> select count(*) as number from cy;
+--------+
| number |
+--------+
| 8 |
+--------+
1 row in set (0.00 sec)
#不用as也可以,一样显示
mysql> select count(*) number from cy;
+--------+
| number |
+--------+
| 8 |
+--------+
1 row in set (0.00 sec)
使用场景:
此外,AS 还可以作为连接语句的操作符。
#创建t1表,将info表的查询记录全部插入t1表
mysql> create table t1 as select * from cy;
Query OK, 8 rows affected (0.02 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
| 6 | hanmeimei | 39.00 | nanjing | 3 |
| 7 | lilei | 22.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
#此处AS起到的作用:
1、创建了一个新表t1 并定义表结构,插入表数据(与info表相同)
2、但是”约束“没有被完全”复制“过来 #但是如果原表设置了主键,那么附表的:default字段会默认设置一个0
相似:
#克隆、复制表结构
mysql> create table t1(select * from cy);
Query OK, 8 rows affected (0.02 sec)
Records: 8 Duplicates: 0 Warnings: 0
#也可以加入where 语句判断
create table test1 as select * from cy where score >=60;
在为表设置别名时,要保证别名不能与数据库中的其他表的名称冲突。
列的别名是在结果中有显示的,而表的别名在结果中没有显示,只在执行查询时使用。
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。
通常通配符都是跟 LIKE(模糊查询) 一起使用的,并协同 WHERE 子句共同来完成查询任务。常用的通配符有两个,分别是:
%:百分号表示零个、一个或多个字符 *
_:下划线表示单个字符 .
#查询名字是c开头的记录
mysql> select id,name from cy where name like 'c%';
+----+--------+
| id | name |
+----+--------+
| 8 | caicai |
+----+--------+
1 row in set (0.00 sec)
#查询名字中间有g的记录
mysql> select id,name from cy where name like '%g%';
+----+--------+
| id | name |
+----+--------+
| 2 | wangwu |
+----+--------+
1 row in set (0.00 sec)
#查询hanmei后面3个字符的名字记录
mysql> select id,name from cy where name like 'hanmei___';
+----+-----------+
| id | name |
+----+-----------+
| 6 | hanmeimei |
+----+-----------+
1 row in set (0.00 sec)
#通配符“%”和“_”不仅可以单独使用,也可以组合使用
查询名字以s开头的记录
mysql> select id,name from cy where name like 'c%_';
+----+--------+
| id | name |
+----+--------+
| 8 | caicai |
+----+--------+
1 row in set (0.00 sec)
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语
句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一步的查询过滤。
#相同表示例:
select name,score from cy where id in (select id from cy where score >80);
主语句:select name,score from cy where id
子语句(集合): select id from cy where score >80
PS:子语句中的sql语句是为了,最后过滤出一个结果集,用于主语句的判断条件
in: 将主表和子表关联/连接的语法
#不同表/多表示例:
先创建一个ky13表。
create table ky13 (id int(4));
insert into ky13 values(1),(2),(3);
mysql> select * from ky13;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
#多表查询
mysql> select id,name,score from cy where id in(select * from ky13);
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 3 | lisi | 60.00 |
+----+--------+-------+
3 rows in set (0.00 sec)
子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE
中也同样适用。在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套。
#将t1里的记录全部删除,重新插入info表的记录
mysql> insert into t1 select * from cy where id in(select id from cy);
Query OK, 8 rows affected (0.00 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
| 6 | hanmeimei | 39.00 | nanjing | 3 |
| 7 | lilei | 22.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
#UPDATE 语句也可以使用子查询。UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列。
#注:创建ky11表的Id,将caicai的分数改为50
mysql> update cy set score=50 where id in(select * from ky13 where id=2);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from cy;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
| 6 | hanmeimei | 39.00 | nanjing | 3 |
| 7 | lilei | 22.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
#DELETE 也适用于子查询
删除分数大于80的记录
mysql> delete from cy where id in(select id where score>80);
Query OK, 2 rows affected (0.01 sec)
mysql> select * from cy;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 50.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 6 | hanmeimei | 39.00 | nanjing | 3 |
| 7 | lilei | 22.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
6 rows in set (0.00 sec)
在 IN 前面还可以添加 NOT,其作用与 IN 相反,表示否定(即不在子查询的结果集里面)
删除分数不是大于等于80的记录
mysql> delete from cy where id not in(select id where score>80);
Query OK, 6 rows affected (0.00 sec)
mysql> select * from cy;
Empty set (0.00 sec)
EXISTS 这个关键字在子查询时,主要用于判断exists之后的条件是否成立,如果成立,则正常执行主语句的匹配,如不成立,则不会执行主语句查询,
如子查询结果集不成立的话,输出为null
#案列 查询如果存在分数等于80的记录则计算t1的字段数
mysql> select count(*) from t1 where exists(select id from t1 where score=80);
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.01 sec)
#查询如果存在分数小于10的记录则计算t1的字段数,t1表没有小于10的,所以返回0
mysql> select count(*) from t1 where exists(select id from t1 where score<10);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
查询t1表id,name 字段
select id,name from t1;
以上命令可以查看到info表的内容
#将结果集做为一张表进行查询的时候,我们也需要用到别名,示例:
需求:从info表中的id和name字段的内容做为"内容" 输出id的部分
mysql> select id from (select id,name from t1);
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 t1) a;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+----+
8 rows in set (0.01 sec)
相当于
select info.id,name from t1;
1.使操作简单化,可以对经常使用的查询定义一个视图,使用户不必为同样的查询操作指定条件
2.增加数据的安全性,通过视图,用户只能查询和修改指定的数据。
3.提高表的逻辑独立性,视图可以屏蔽原有表结构变化带来的影响。
总结:使用视图的大部分情况是为了保障数据安全性,提高查询效率
语法: CREATE VIEW "视图表名" AS "SELECT 语句";
示例:
需求:满足80分的学生展示在视图中
PS:这个结果会动态变化,同时可以给不同的人群(例如权限范围)展示不同的视图
#创建视图
mysql> create view v_score as select * from t1 where score>=80;
Query OK, 0 rows affected (0.01 sec)
show table status\G
#查看视图
mysql> select * from v_score;
+----+----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
+----+----------+-------+------------+-------+
4 rows in set (0.00 sec)
mysql> update t1 set score='60' where name='wangwu'; #修改原表数据
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from v_score; #查看视图
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 95.00 | hangzhou | 5 |
| 5 | jiaoshou | 93.00 | laowo | 3 |
+----+----------+-------+----------+-------+
3 rows in set (0.00 sec)
查询info表结构,id和name字段是不允许空值的
null值与空值的区别(空气与真空)
1.无值的长度为0,不占用空间的;而NULL 值的长度是NULL,是占用空间的。
2.IS NULL或者IS NOT NULL,是用来判断字段是不是为NULL或者不是NULL,不能查出是不是无值的。
3.无值的判断使用=' '或者<>' '来处理。<>代表不等于。{}
4.在通过count()指定字段统计有多少行数时,如果遇到NULL 值会自动忽略掉,遇到无值会加入到记录中进行计算。
mysql> desc t1;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | 0 | |
| name | varchar(20) | YES | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| hobby | varchar(20) | YES | | NULL | |
| addr | varchar(40) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
插入一条记录,分数字段输入null,显示出来就是null
#验证:
mysql> alter table t1 add column addr varchar(40);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(10) | NO | | 0 | |
| name | varchar(20) | YES | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| hobby | varchar(20) | YES | | NULL | |
| addr | varchar(40) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> select * from t1;
+----+-----------+-------+------------+-------+------+
| id | name | score | address | hobby | addr |
+----+-----------+-------+------------+-------+------+
| 1 | liuyi | 80.00 | beijing | 2 | NULL |
| 2 | wangwu | 60.00 | shengzheng | 2 | NULL |
| 3 | lisi | 60.00 | shanghai | 4 | NULL |
| 4 | tianqi | 95.00 | hangzhou | 5 | NULL |
| 5 | jiaoshou | 93.00 | laowo | 3 | NULL |
| 6 | hanmeimei | 39.00 | nanjing | 3 | NULL |
| 7 | lilei | 22.00 | nanjing | 5 | NULL |
| 8 | caicai | 16.00 | nanjing | 5 | NULL |
+----+-----------+-------+------------+-------+------+
8 rows in set (0.00 sec)
mysql> update t1 set addr='nj' where score >=70;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from t1;
+----+-----------+-------+------------+-------+------+
| id | name | score | address | hobby | addr |
+----+-----------+-------+------------+-------+------+
| 1 | liuyi | 80.00 | beijing | 2 | nj |
| 2 | wangwu | 60.00 | shengzheng | 2 | NULL |
| 3 | lisi | 60.00 | shanghai | 4 | NULL |
| 4 | tianqi | 95.00 | hangzhou | 5 | nj |
| 5 | jiaoshou | 93.00 | laowo | 3 | nj |
| 6 | hanmeimei | 39.00 | nanjing | 3 | NULL |
| 7 | lilei | 22.00 | nanjing | 5 | NULL |
| 8 | caicai | 16.00 | nanjing | 5 | NULL |
+----+-----------+-------+------------+-------+------+
8 rows in set (0.00 sec)
#统计数量:检测null是否会加入统计中
mysql> select count(addr) from t1;
+-------------+
| count(addr) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)
#将info表中其中一条数据修改为空值''
mysql> update t1 set addr='' where name='wnagwu';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
#统计数量,检测空值是不会被添加到统计中
mysql> select count(addr) from t1;
+-------------+
| count(addr) |
+-------------+
| 3 |
+-------------+
#查询null值
mysql> select * from t1 where addr is null;
+----+-----------+-------+------------+-------+------+
| id | name | score | address | hobby | addr |
+----+-----------+-------+------------+-------+------+
| 2 | wangwu | 60.00 | shengzheng | 2 | NULL |
| 3 | lisi | 60.00 | shanghai | 4 | NULL |
| 6 | hanmeimei | 39.00 | nanjing | 3 | NULL |
| 7 | lilei | 22.00 | nanjing | 5 | NULL |
| 8 | caicai | 16.00 | nanjing | 5 | NULL |
+----+-----------+-------+------------+-------+------+
5 rows in set (0.00 sec)
空值数据: select count(*) from 表名 where 字段名 is null
#查询不为空的值
mysql> select * from t1 where addr is not null;
+----+----------+-------+----------+-------+------+
| id | name | score | address | hobby | addr |
+----+----------+-------+----------+-------+------+
| 1 | liuyi | 80.00 | beijing | 2 | nj |
| 4 | tianqi | 95.00 | hangzhou | 5 | nj |
| 5 | jiaoshou | 93.00 | laowo | 3 | nj |
+----+----------+-------+----------+-------+------+
3 rows in set (0.00 sec)
非空值数据: select count(*) from 表名 where 字段名 is not null