Mysql的高阶语句——排序、分组、别名、通配符、子查询、视图、null值

1. 常用查询

1.1 排序

1.1.1 按照关键字排序

##order by 语法结构
select 字段1,字段2,字段3…… from 表名 where 条件表达式 order by 字段1,字段2 ascdesc;
查看某表的字段123,并根据字段12进行排序

ASC 是按照升序进行排序的,是默认的排序方式,即 ASC 可以省略。SELECT 语句中如果没有指定具体的排序方式,则默认按 ASC方式进行排序。
DESC 是按降序方式进 行排列。当然 ORDER BY 前面也可以使用 WHERE 子句对查询结果进一步过滤。

mysql> create table fenshu (
    -> id int(5),
    -> name varchar(10) primary key not null ,
    -> score decimal(5,2),
    -> address varchar(20),
    -> hobbid int(5));
Query OK, 0 rows affected (0.02 sec)

insert into fenshu values(1,'a',90,'nanjing',1);
insert into fenshu values(2,'h',90,'shanghai',2);
insert into fenshu values(3,'d',70,'shanghai',3);
insert into fenshu values(4,'e',99,'xuzhou',5);
insert into fenshu values(5,'w',98,'xuzhou',4);
insert into fenshu values(6,'c',10,'wuxi',3);
insert into v values(7,'z',11,'wuxi',5);

mysql> select *from fenshu;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

#按hobbid升序
mysql> select *from fenshu order by hobbid asc;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    2 | h    | 90.00 | shanghai |      2 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    5 | w    | 98.00 | xuzhou   |      4 |
|    4 | e    | 99.00 | xuzhou   |      5 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

#按hobbid降序
mysql> select *from fenshu order by hobbid desc;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    4 | e    | 99.00 | xuzhou   |      5 |
|    5 | w    | 98.00 | xuzhou   |      4 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    2 | h    | 90.00 | shanghai |      2 |
|    1 | a    | 90.00 | nanjing  |      1 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

1.1.2 多字段排序

#可以结合where进行条件过滤
#对上海的学生的成绩做降序排列
mysql> select *from fenshu where address='shanghai' order by score desc;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    2 | h    | 90.00 | shanghai |      2 |
|    3 | d    | 70.00 | shanghai |      3 |
+------+------+-------+----------+--------+
2 rows in set (0.01 sec)

#查询学生信息先按hobbid降序排列,在这基础上id也按降序排列
mysql> select *from fenshu order by hobbid desc,id desc;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    4 | e    | 99.00 | xuzhou   |      5 |
|    5 | w    | 98.00 | xuzhou   |      4 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    2 | h    | 90.00 | shanghai |      2 |
|    1 | a    | 90.00 | nanjing  |      1 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

1.1.3 区间判断及查询不重复记录

1.1.3.1 AND/OR ——且/或

and:同时满足
or:只要满足一个条件都会进行匹配

##查询www表内分数大于60且小于90分
mysql> select *from fenshu where score > 60 and score <90;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    3 | d    | 70.00 | shanghai |      3 |
+------+------+-------+----------+--------+
1 row in set (0.00 sec)
1.1.3.2 嵌套/多条件
##筛选出来的是大于70的所有分数
mysql> select *from fenshu where score > 70 or (score > 75 and score < 90);
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
4 rows in set (0.00 sec)

1.1.3.3 添加-distinct

distinct 查询不重复记录

语法格式:
select distinct 字段 from 表名;

#查询表内不重复的数据,
mysql> select distinct hobbid from fenshu;
+--------+
| hobbid |
+--------+
|      1 |
|      3 |
|      5 |
|      2 |
|      4 |
+--------+
5 rows in set (0.00 sec)

mysql> select distinct hobbid,id,name from fenshu;
+--------+------+------+
| hobbid | id   | name |
+--------+------+------+
|      1 |    1 | a    |
|      3 |    6 | c    |
|      3 |    3 | d    |
|      5 |    4 | e    |
|      2 |    2 | h    |
|      4 |    5 | w    |
+--------+------+------+
6 rows in set (0.01 sec)

1.1.4 对结果进行分组

语法格式:
SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator valueGROUP BY column_name;

#按hobbid相同的分组,计算相同分数的学生个数(基于name个数进行计数)
mysql> select count(name),hobbid from fenshu group by hobbid;
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
|           1 |      1 |
|           1 |      2 |
|           2 |      3 |
|           1 |      4 |
|           1 |      5 |
+-------------+--------+
5 rows in set (0.00 sec)

#结合where语句,筛选分数大于等于80的分组,计算学生个数
mysql> select count(name),hobbid from fenshu where score >80 group by hobbid; 
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
|           1 |      1 |
|           1 |      2 |
|           1 |      4 |
|           1 |      5 |
+-------------+--------+
4 rows in set (0.00 sec)

#结合order by把计算出的学生个数按升序排列
mysql> select count(name),score,hobbid from fenshu where score >= 80 group by hobbid order by count(name) asc;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
|           1 | 90.00 |      1 |
|           1 | 99.00 |      5 |
|           1 | 90.00 |      2 |
|           1 | 98.00 |      4 |
+-------------+-------+--------+
4 rows in set (0.00 sec)

1.1.5 限制结果条目

语法格式:
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number

mysql> select *from fenshu;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

##查询所有信息显示前四行记录
mysql> select *from fenshu limit 3;	
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
+------+------+-------+----------+--------+
3 rows in set (0.00 sec)

#从第4行开始,往后显示3行内容
mysql> select *from fenshu limit 3,3;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
3 rows in set (0.00 sec)

#成绩大于80的且显示第二行开始的后两条记录
mysql> select id,name,score from fenshu where score >=80 limit 2;
+------+------+-------+
| id   | name | score |
+------+------+-------+
|    1 | a    | 90.00 |
|    4 | e    | 99.00 |
+------+------+-------+
2 rows in set (0.00 sec)

##结合order by语句,按id的大小升序排列显示前三行
mysql> select id,name from fenshu order by id limit 3;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | h    |
|    3 | d    |
+------+------+
3 rows in set (0.00 sec)

1.2 设置别名–alias

在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性

语法格式:
对于列的别名:SELECT column_name AS alias_nameFROM table_name;

对于表的别名:SELECT column_name(s)FROM table_name AS alias_name;

#查询info表的字段数量,以number显示
mysql> select count(*) as number from fenshu;
+--------+
| number |
+--------+
|      6 |
+--------+
1 row in set (0.00 sec)

#给表临时设置一个别名i ,进行查看
mysql> select i.id,i.name from fenshu as i;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    6 | c    |
|    3 | d    |
|    4 | e    |
|    2 | h    |
|    5 | w    |
+------+------+
6 rows in set (0.00 sec)
#设置别名的优点在于
对复杂的表进行查询的时候,别名可以缩短查询语句的长度
多表相连查询的时候(通俗易懂、减短sql语句)
此外,AS 还可以作为连接语句的操作符。

#创建f1表,将fenshu表的查询记录全部插入f1表
mysql> create table f1 as select *from fenshu;
Query OK, 6 rows affected (0.02 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select *from f1;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

1.3 通配符

通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。

通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。常用的通配符有两个,分别是:

%:百分号表示零个、一个或多个字符
_:下划线表示单个字符

#查询地址中带n的字幕
mysql> select id,name,address from fenshu where address like '%n%';
+------+------+----------+
| id   | name | address  |
+------+------+----------+
|    1 | a    | nanjing  |
|    3 | d    | shanghai |
|    2 | h    | shanghai |
+------+------+----------+
3 rows in set (0.01 sec)

#查询地址中w和g中间有2个字符的记录
mysql> select id,name,address from f1 where address like 's_ang_%';
+------+------+----------+
| id   | name | address  |
+------+------+----------+
|    3 | d    | shanghai |
|    2 | h    | shanghai |
+------+------+----------+
2 rows in set (0.00 sec)

#查询xu后面4个字符的名字记录
mysql> select id,name,address from f1 where address like 'xu____';
+------+------+---------+
| id   | name | address |
+------+------+---------+
|    4 | e    | xuzhou  |
|    5 | w    | xuzhou  |
+------+------+---------+
2 rows in set (0.00 sec)

1.4 子查询

子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE 中也同样适用。在嵌套的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套

当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。 若启用了 NOT 关键字,则返回值相反。需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的

子查询还可以用在 INSERT 语句中。子查询的结果集可以通过 INSERT 语句插入到其 他的表中

1.4.1 select 语句的使用

语法格式:
IN 用来判断某个值是否在给定的结果集中,通常结合子查询来使用

语法:
<表达式> [NOT] IN <子查询>

#查询分数大于80的记录
mysql> select name,score from fenshu where id in (select id from fenshu where score >80);
+------+-------+
| name | score |
+------+-------+
| a    | 90.00 |
| e    | 99.00 |
| h    | 90.00 |
| w    | 98.00 |
+------+-------+
4 rows in set (0.00 sec)

#新建一个表,达到多表查询
mysql> create table f2(id int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into f2 values(1),(2),(3),(4);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from f2;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

mysql> select id,name score from fenshu where id in (select *from f2);
+------+-------+
| id   | score |
+------+-------+
|    1 | a     |
|    3 | d     |
|    4 | e     |
|    2 | h     |
+------+-------+
4 rows in set (0.01 sec)

1.4.2 多层嵌套

#查询分数大于80的记录
mysql> select name,score from fenshu where id in (select id from fenshu where score > 80);
+------+-------+
| name | score |
+------+-------+
| a    | 90.00 |
| e    | 99.00 |
| h    | 90.00 |
| w    | 98.00 |
+------+-------+
4 rows in set (0.00 sec)


mysql> delete from f1;
Query OK, 6 rows affected (0.00 sec)

mysql> insert into f1 select *from fenshu where id in (select id from fenshu);
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select *from f1;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)


#UPDATE 内的子查询,在 set 更新内容时,可以是单独的一列,也可以是多列
mysql> update fenshu set score =50 where id in (select *from f2 where id=4);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from fenshu;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    6 | c    | 10.00 | wuxi     |      3 |
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 50.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
6 rows in set (0.00 sec)

#在 IN 前面还可以添加 NOT,其作用与 IN 相反,表示否定(即不在子查询的结果集里面)
#表示 先匹配出f2表内的id字段为基础匹配的结果集(2,3,4)
#然后再执行主语句,以主语句的id为基础 进行where 条件判断/过滤
mysql> update fenshu set score =100 where id not in (select *from f2 where id > 1);
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select *from fenshu;
+------+------+--------+----------+--------+
| id   | name | score  | address  | hobbid |
+------+------+--------+----------+--------+
|    1 | a    | 100.00 | nanjing  |      1 |
|    6 | c    | 100.00 | wuxi     |      3 |
|    3 | d    |  70.00 | shanghai |      3 |
|    4 | e    |  50.00 | xuzhou   |      5 |
|    2 | h    |  90.00 | shanghai |      2 |
|    5 | w    | 100.00 | xuzhou   |      4 |
+------+------+--------+----------+--------+
6 rows in set (0.00 sec)

#删除大于80分
mysql> delete from fenshu where id in ( select id where score > 80);
Query OK, 4 rows affected (0.00 sec)

mysql> select *from fenshu;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 50.00 | xuzhou   |      5 |
+------+------+-------+----------+--------+
2 rows in set (0.00 sec)

#EXISTS 这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
mysql> select *from fenshu;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    3 | d    | 70.00 | shanghai |      3 |
|    4 | e    | 50.00 | xuzhou   |      5 |
+------+------+-------+----------+--------+
2 rows in set (0.00 sec)

mysql> select count(*) from fenshu where exists(select id from fenshu where score=70);
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from fenshu where exists(select id from fenshu where score>100);
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

2. 视图

2.1 视图概述

视图是一张虚拟的表,数据不存在视图中,视图是真实表的映射数据。例如:水中捞月是典型的视图
利用条件筛选,分组,排序等产生出一个结果集(结果集保存在内存中),并且做成持久化保存(保存映射)
视图占用资源小,真实表中数据产生变化,会影响到视图。

特点:
安全性高
简化sql操作
可以针对不同用户创建不同的视图(不同权限的用户浏览不同的信息)

2.2 视图和表的区别和联系

区别
视图是已经编译好的sql语句;而表不是
视图没有实际的物理记录;而表有
表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改, 但视图只能有创建的语句来修改
视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表
视图的建立和删除只影响视图本身,不影响对应的基本表。
联系
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自表,它依据基本表存在而存在
一个视图可以对应一个基本表,也可以对应多个基本表。视图本表的抽象和在逻辑意义上建立的新关系。

2.3 创建和查询视图

创建视图
create view <视图名称>
create view info_view as select id,name,age from info 条件
多表相连的条件是on
单表相连的条件是where
查询视图
select * from info_view;
select name,age from info_view;

2.3.1 单表创建视图


mysql> create view v_score as select *from f1 where score >=80;
Query OK, 0 rows affected (0.02 sec)

mysql> select *from v_score;
+------+------+-------+----------+--------+
| id   | name | score | address  | hobbid |
+------+------+-------+----------+--------+
|    1 | a    | 90.00 | nanjing  |      1 |
|    4 | e    | 99.00 | xuzhou   |      5 |
|    2 | h    | 90.00 | shanghai |      2 |
|    5 | w    | 98.00 | xuzhou   |      4 |
+------+------+-------+----------+--------+
4 rows in set (0.00 sec)

mysql> update f1 set score = '100' where name = 'e';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from v_score;
+------+------+--------+----------+--------+
| id   | name | score  | address  | hobbid |
+------+------+--------+----------+--------+
|    1 | a    |  90.00 | nanjing  |      1 |
|    4 | e    | 100.00 | xuzhou   |      5 |
|    2 | h    |  90.00 | shanghai |      2 |
|    5 | w    |  98.00 | xuzhou   |      4 |
+------+------+--------+----------+--------+
4 rows in set (0.00 sec)

2.3.2 多表创建视图

mysql> create view v_f1(id,name,score,age) as select f1.id,f1.name,f1.score,f11.age from f1,f11 where f1.name=f11.name;
Query OK, 0 rows affected (0.01 sec)
#创建视图 视图叫v_组合,字段是id、name、score、test01.age来源于f1.id、f1.name、f1.score、f11.age,来自于f1和f11表,同时where判断f1表中的name等于f11表内的name字段

mysql> select *from v_f1;                                                              
+------+------+-------+------+
| id   | name | score | age  |
+------+------+-------+------+
|    1 | a    | 90.00 | 10   |
|    2 | h    | 90.00 | 30   |
+------+------+-------+------+
2 rows in set (0.00 sec)

2.3.3 修改表update

mysql> update f1 set score=95 where name ='a';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from v_f1;
+------+------+-------+------+
| id   | name | score | age  |
+------+------+-------+------+
|    1 | a    | 95.00 | 10   |
|    2 | h    | 90.00 | 30   |
+------+------+-------+------+
2 rows in set (0.00 sec)

mysql> select *from f1;
+------+------+--------+----------+--------+
| id   | name | score  | address  | hobbid |
+------+------+--------+----------+--------+
|    1 | a    |  95.00 | nanjing  |      1 |
|    6 | c    |  10.00 | wuxi     |      3 |
|    3 | d    |  70.00 | shanghai |      3 |
|    4 | e    | 100.00 | xuzhou   |      5 |
|    2 | h    |  90.00 | shanghai |      2 |
|    5 | w    |  98.00 | xuzhou   |      4 |
+------+------+--------+----------+--------+
6 rows in set (0.00 sec)

#更改视图,源文件里面的内容也会进行更改

3. NULL 值

在 SQL 语句使用过程中,经常会碰到 NULL 这几个字符。通常使用 NULL 来表示缺失 的值,也就是在表中该字段是没有值的。如果在创建表时,限制某些字段不为空,则可以使 用 NOT NULL 关键字,不使用则默认可以为空。在向表内插入记录或者更新记录时,如果该字段没有 NOT NULL 并且没有值,这时候新记录的该字段将被保存为 NULL。需要注意 的是,NULL 值与数字 0 或者空白(spaces)的字段是不同的,值为 NULL 的字段是没有 值的。在 SQL 语句中,使用 IS NULL 可以判断表内的某个字段是不是 NULL 值,相反的用 IS NOT NULL 可以判断不是 NULL 值

#插入一个记录,查看下显示出来的是否为null 值
mysql> alter table f1 add column age char(20);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc f1;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(5)       | YES  |     | NULL    |       |
| name    | varchar(10)  | NO   |     | NULL    |       |
| score   | decimal(5,2) | YES  |     | NULL    |       |
| address | varchar(20)  | YES  |     | NULL    |       |
| hobbid  | int(5)       | YES  |     | NULL    |       |
| age     | char(20)     | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

#将分数小于60的改为age 是100
mysql> update f1 set age=100 where score <60;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from f1;
+------+------+--------+----------+--------+------+
| id   | name | score  | address  | hobbid | age  |
+------+------+--------+----------+--------+------+
|    1 | a    |  95.00 | nanjing  |      1 | NULL |
|    6 | c    |  10.00 | wuxi     |      3 | 100  |
|    3 | d    |  70.00 | shanghai |      3 | NULL |
|    4 | e    | 100.00 | xuzhou   |      5 | NULL |
|    2 | h    |  90.00 | shanghai |      2 | NULL |
|    5 | w    |  98.00 | xuzhou   |      4 | NULL |
+------+------+--------+----------+--------+------+
6 rows in set (0.00 sec)

#统计数量:检测null不会加入统计中
mysql> select count(age) from f1;
+------------+
| count(age) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

#查询是否为null值
mysql> select *from f1 where age is null;
+------+------+--------+----------+--------+------+
| id   | name | score  | address  | hobbid | age  |
+------+------+--------+----------+--------+------+
|    1 | a    |  95.00 | nanjing  |      1 | NULL |
|    3 | d    |  70.00 | shanghai |      3 | NULL |
|    4 | e    | 100.00 | xuzhou   |      5 | NULL |
|    2 | h    |  90.00 | shanghai |      2 | NULL |
|    5 | w    |  98.00 | xuzhou   |      4 | NULL |
+------+------+--------+----------+--------+------+
5 rows in set (0.00 sec)

mysql> select *from f1 where age is not null;
+------+------+-------+---------+--------+------+
| id   | name | score | address | hobbid | age  |
+------+------+-------+---------+--------+------+
|    6 | c    | 10.00 | wuxi    |      3 | 100  |
+------+------+-------+---------+--------+------+
1 row in set (0.00 sec)

#空值长度为0,不占空间,NULL值的长度为null,占用空间
#is null无法判断空值
#空值使用"=“或者”<>"来处理(!=)
#以count() 统计行数的时候,null值的记录行反而不会被统计在内,而空值的记录行会被统计在内
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值