mysql的高阶语句介绍(排序,查询和视图)

mysql高阶语句介绍(正则和运算)

mysql高阶语句(函数)

mysql的高阶语句介绍(排序,查询和视图)

mysql的高阶语句介绍

  • 按关键字排序
    • 升序排列
    • 降序排列
    • 结合判断语句排序
    • 多字段的排序
  • 区间判断查询
  • 去重查询
  • 对结果进行分组
    • 结合函数分组
    • 结合排序分组
  • 限制结果的条目
    • 结合排序限制
  • 设置别名查询
    • as设置别名
    • as作为克隆作用
  • 通配符
  • 子查询
    • 相同表子查询
    • 不同表查询
    • 不同表插入
    • 修改时使用子查询
    • 删除时使用子查询
    • existe结合子查询
    • 别名的子查询
  • 数据库的视图
    • 单表视图
    • 多表视图
  • null值
    • null值与空值的区别

mysql除了增删改查之外,有时候需要对查询结果进行处理,这就衍生出了高级的查询语句

按关键字排序

  • 对查询结果进行排序

升序排列

  • asc:是按照升序进行排序,默认的排序方式,可以省略
mysql> select * from test;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 60.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)

mysql> select id,name,score from test order by score;	//指定score默认排序是升序
+------+-----------+-------+
| id   | name      | score |
+------+-----------+-------+
|    6 | hanmeimei | 40.00 |
|    7 | lilei     | 50.00 |
|    3 | wangwu    | 60.00 |
|    1 | zhangsan  | 80.00 |
|    2 | lisi      | 90.00 |
|    5 | wangdi    | 98.00 |
|    4 | tianqi    | 99.00 |
+------+-----------+-------+
7 rows in set (0.00 sec)

降序排列

  • desc:是按降序方式进行排列,需要指定
mysql> select id,name,score from test order by score desc;	//查询score指定为降序方式
+------+-----------+-------+
| id   | name      | score |
+------+-----------+-------+
|    4 | tianqi    | 99.00 |
|    5 | wangdi    | 98.00 |
|    2 | lisi      | 90.00 |
|    1 | zhangsan  | 80.00 |
|    3 | wangwu    | 60.00 |
|    7 | lilei     | 50.00 |
|    6 | hanmeimei | 40.00 |
+------+-----------+-------+
7 rows in set (0.00 sec)

结合判断语句排序

mysql> select id,name,score from test where address='nanjing' order by score desc;	//指定address=南京的score以降序排列
+------+-----------+-------+
| id   | name      | score |
+------+-----------+-------+
|    7 | lilei     | 50.00 |
|    6 | hanmeimei | 40.00 |
+------+-----------+-------+
2 rows in set (0.00 sec)

多字段的排序

  • order by后面可以跟多个字段排序,优先级按照先后顺序,必须要前一个字段有相同的记录才能是第二个字段排序有意义
mysql> select id,name,hobbid from test order by hobbid desc,id desc;	//指定hobbid降序排列,相同的hobbid中id降序排列
+------+-----------+--------+
| id   | name      | hobbid |
+------+-----------+--------+
|    7 | lilei     |      5 |
|    4 | tianqi    |      5 |
|    3 | wangwu    |      4 |
|    6 | hanmeimei |      3 |
|    5 | wangdi    |      3 |
|    2 | lisi      |      2 |
|    1 | zhangsan  |      2 |
+------+-----------+--------+
7 rows in set (0.00 sec)

mysql> select id,name,hobbid from test order by hobbid desc,id;	//默认不加desc就是升序排列
+------+-----------+--------+
| id   | name      | hobbid |
+------+-----------+--------+
|    4 | tianqi    |      5 |
|    7 | lilei     |      5 |
|    3 | wangwu    |      4 |
|    5 | wangdi    |      3 |
|    6 | hanmeimei |      3 |
|    1 | zhangsan  |      2 |
|    2 | lisi      |      2 |
+------+-----------+--------+
7 rows in set (0.00 sec)

区间判断查询

mysql> select * from test where score >50 and score <=90;	//查询score在大于50和小于等于90中间
+------+----------+-------+------------+--------+
| id   | name     | score | address    | hobbid |
+------+----------+-------+------------+--------+
|    2 | lisi     | 90.00 | shengzheng |      2 |
|    3 | wangwu   | 60.00 | shanghai   |      4 |
|    1 | zhangsan | 80.00 | beijing    |      2 |
+------+----------+-------+------------+--------+
3 rows in set (0.00 sec)

mysql> select * from test where score >50 or score <=90;	//查询score在大于50或者小于等于90的记录,所以都满足
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 60.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)

mysql> select * from test where score >50 or (score >40 and score <90);	//先执行括号的句子,括号取值为大于40和小于等于90的score记录,然后再跟大于50取或,最小值为50,所以最终为大于50
+------+----------+-------+------------+--------+
| id   | name     | score | address    | hobbid |
+------+----------+-------+------------+--------+
|    7 | lilei    | 50.00 | nanjing    |      5 |
|    2 | lisi     | 90.00 | shengzheng |      2 |
|    4 | tianqi   | 99.00 | hangzhou   |      5 |
|    5 | wangdi   | 98.00 | laowo      |      3 |
|    3 | wangwu   | 60.00 | shanghai   |      4 |
|    1 | zhangsan | 80.00 | beijing    |      2 |
+------+----------+-------+------------+--------+
6 rows in set (0.00 sec)

去重查询

mysql> select distinct hobbid from test;	//将重复的去掉不显示
+--------+
| hobbid |
+--------+
|      3 |
|      5 |
|      2 |
|      4 |
+--------+
4 rows in set (0.00 sec)

对结果进行分组

  • 使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理

结合函数分组

mysql> select count(name),hobbid from test group by hobbid;	//将hobbid分组并计数
+-------------+--------+
| count(name) | hobbid |
+-------------+--------+
|           2 |      2 |
|           2 |      3 |
|           1 |      4 |
|           2 |      5 |
+-------------+--------+
4 rows in set (0.00 sec)

结合排序分组

mysql> select count(name),score,hobbid from test where score>=80 group by hobbid order by count(name);	//将hobbid分组并计数然后计数默认升序排列
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
|           1 | 99.00 |      5 |
|           1 | 98.00 |      3 |
|           1 | 80.00 |      4 |
|           2 | 90.00 |      2 |
+-------------+-------+--------+
4 rows in set (0.00 sec)

限制结果的条目

  • LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示,如果是1就是第二行,0为第一行,第一行是字段名。
mysql> select * from test limit 4;	//显示五行,第一行是字段名,实际显示是四行记录
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
+------+-----------+-------+------------+--------+
4 rows in set (0.00 sec)


mysql> select * from test limit 4,2;	//从第五行起,查询后面两行
+------+--------+-------+----------+--------+
| id   | name   | score | address  | hobbid |
+------+--------+-------+----------+--------+
|    5 | wangdi | 98.00 | laowo    |      3 |
|    3 | wangwu | 80.00 | shanghai |      4 |
+------+--------+-------+----------+--------+
2 rows in set (0.00 sec)

结合排序限制

mysql> select id,name from test order by id limit 3;		//查询id,name并以升序排列id并限制只查询四行
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
|    2 | lisi     |
|    3 | wangwu   |
+------+----------+
3 rows in set (0.00 sec)

mysql> select id,name from test order by id desc limit 3;	//将查询的记录倒序,并只查四行,也就是查记录的最后三行
+------+-----------+
| id   | name      |
+------+-----------+
|    7 | lilei     |
|    6 | hanmeimei |
|    5 | wangdi    |
+------+-----------+
3 rows in set (0.00 sec)

设置别名查询

as设置别名

  • 在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者多次使用相同的表,可以给字段列或表设置别名
mysql> select count(id) as member from test;	//统计id总数并设置字段别名为member
+--------+
| member |
+--------+
|      7 |
+--------+
1 row in set (0.00 sec)

mysql> select count(id) member from test;	//中间的as可以省略
+--------+
| member |
+--------+
|      7 |
+--------+
1 row in set (0.00 sec)

as作为克隆作用

mysql> create table test1 as select * from test;	用as相当于克隆test表,但是约束并不会被复制
Query OK, 7 rows affected (0.09 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
mysql> desc test;
+---------+--------------+------+-----+---------+-------+
| 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> desc test1;
+---------+--------------+------+-----+---------+-------+
| 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> create table test2 as select * from test where score >=60;	//也可以加上where来判断复制的内容
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from test2;
+------+----------+-------+------------+--------+
| id   | name     | score | address    | hobbid |
+------+----------+-------+------------+--------+
|    2 | lisi     | 90.00 | shengzheng |      2 |
|    4 | tianqi   | 99.00 | hangzhou   |      5 |
|    5 | wangdi   | 98.00 | laowo      |      3 |
|    3 | wangwu   | 80.00 | shanghai   |      4 |
|    1 | zhangsan | 80.00 | beijing    |      2 |
+------+----------+-------+------------+--------+
5 rows in set (0.00 sec)

通配符

  • 通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来,通常是跟like一起使用,并跟where判断来完成查询

  • 常用的通配符
    %:百分号表示零个、一个或多个字符类似于正则的“*”
    _:下划线表示单个字符,类似正则的“.”

mysql> select * from test;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)

mysql> select id,name from test where name like 'l%';	//查询名字是l开头的记录
+------+-------+
| id   | name  |
+------+-------+
|    7 | lilei |
|    2 | lisi  |
+------+-------+
2 rows in set (0.00 sec)

mysql> select id,name from test where name like 'l_s_';	//查询ls中间任一字符,s后任一字符的名字
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.00 sec)

mysql> select id,name from test where name like '%s%';	//查询中间有s的名字记录
+------+----------+
| id   | name     |
+------+----------+
|    2 | lisi     |
|    1 | zhangsan |
+------+----------+
2 rows in set (0.00 sec)

mysql> select id,name from test where name like 'l___';		//查询l后面有三个任意字符的名字记录
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.00 sec)

mysql> select id,name from test where name like 'l_%';	//查询l后面跟任意字符的名字记录
+------+-------+
| id   | name  |
+------+-------+
|    7 | lilei |
|    2 | lisi  |
+------+-------+
2 rows in set (0.01 sec)

子查询

  • 子查询也被称作内查询或者嵌套查询,在一个查询语句中嵌套另一个查询语句,子查询语句是先于主查询语句被执行,其结果作为外层的条件返回给主查询进行下一 步的查询

相同表子查询

mysql> select name,score from test where id in (select id from test where score >80);	//子语句查询出来的id给外面的主语句来查询,in是将主语句跟子语句中的表关联
+--------+-------+
| name   | score |
+--------+-------+
| lisi   | 90.00 |
| tianqi | 99.00 |
| wangdi | 98.00 |
+--------+-------+
3 rows in set (0.00 sec)
mysql> select id from test where score >80
    -> ;
+------+
| id   |
+------+
|    2 |
|    4 |
|    5 |
+------+
3 rows in set (0.00 sec)

不同表查询

mysql> select id,name,score from test where id in (select id from qq);	//先查询qq表的id然后赋值给主语句查询
+------+----------+-------+
| id   | name     | score |
+------+----------+-------+
|    2 | lisi     | 90.00 |
|    1 | zhangsan | 80.00 |
+------+----------+-------+
2 rows in set (0.00 sec)

mysql> select id from qq;
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)

不同表插入

  • 子查询还可以用在 INSERT 语句中
mysql> select * from test2;
+------+----------+-------+------------+--------+
| id   | name     | score | address    | hobbid |
+------+----------+-------+------------+--------+
|    2 | lisi     | 90.00 | shengzheng |      2 |
|    4 | tianqi   | 99.00 | hangzhou   |      5 |
|    5 | wangdi   | 98.00 | laowo      |      3 |
|    3 | wangwu   | 80.00 | shanghai   |      4 |
|    1 | zhangsan | 80.00 | beijing    |      2 |
+------+----------+-------+------------+--------+
5 rows in set (0.00 sec)

mysql> insert into test2 select * from test where id in (select id from test);	//将test的记录全部插入test2中
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from test2;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
12 rows in set (0.00 sec)

修改时使用子查询

mysql> update test set score=50 where id in (select id from test2 where id=2);	//test对应id为2的score修改为50
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 50.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)

删除时使用子查询

mysql> select * from test;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 50.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)

mysql> delete from test where id in (select id from test2 where score>80);	//查询test2表中的score大于80的id然后对应test中删除
Query OK, 3 rows affected (0.00 sec)

mysql> select * from test;
+------+-----------+-------+----------+--------+
| id   | name      | score | address  | hobbid |
+------+-----------+-------+----------+--------+
|    6 | hanmeimei | 40.00 | nanjing  |      3 |
|    7 | lilei     | 50.00 | nanjing  |      5 |
|    3 | wangwu    | 80.00 | shanghai |      4 |
|    1 | zhangsan  | 80.00 | beijing  |      2 |
+------+-----------+-------+----------+--------+
4 rows in set (0.00 sec)


mysql> select * from test;
+------+-----------+-------+----------+--------+
| id   | name      | score | address  | hobbid |
+------+-----------+-------+----------+--------+
|    6 | hanmeimei | 40.00 | nanjing  |      3 |
|    7 | lilei     | 50.00 | nanjing  |      5 |
|    3 | wangwu    | 80.00 | shanghai |      4 |
|    1 | zhangsan  | 80.00 | beijing  |      2 |
+------+-----------+-------+----------+--------+
4 rows in set (0.00 sec)

mysql> delete from test where id not in (select id where score>=80);	//查询score大于等于80的id然后排除并删除不是这些id的记录
Query OK, 2 rows affected (0.00 sec)

mysql> select * from test;
+------+----------+-------+----------+--------+
| id   | name     | score | address  | hobbid |
+------+----------+-------+----------+--------+
|    3 | wangwu   | 80.00 | shanghai |      4 |
|    1 | zhangsan | 80.00 | beijing  |      2 |
+------+----------+-------+----------+--------+
2 rows in set (0.00 sec)


existe结合子查询

mysql> select count(*) from test1 where exists(select id from test1 where score=80);	//查询score等于80的id,如果有就执行主语句
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from test1 where exists(select id from test1 where score<40);	//查询scorexiaoyu40的id,没有就不执行前面的主语句
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> select * from test1;
+------+-----------+-------+------------+--------+
| id   | name      | score | address    | hobbid |
+------+-----------+-------+------------+--------+
|    6 | hanmeimei | 40.00 | nanjing    |      3 |
|    7 | lilei     | 50.00 | nanjing    |      5 |
|    2 | lisi      | 90.00 | shengzheng |      2 |
|    4 | tianqi    | 99.00 | hangzhou   |      5 |
|    5 | wangdi    | 98.00 | laowo      |      3 |
|    3 | wangwu    | 80.00 | shanghai   |      4 |
|    1 | zhangsan  | 80.00 | beijing    |      2 |
+------+-----------+-------+------------+--------+
7 rows in set (0.00 sec)

别名的子查询

  • 将结果集做为一张表进行查询的时候,我们需要用到别名,不然会报错
mysql> select id from (select id,name from test);
ERROR 1248 (42000): Every derived table must have its own alias

mysql> select a.id,name from (select id,name from test) a;	//将a作为别名
+------+----------+
| id   | name     |
+------+----------+
|    3 | wangwu   |
|    1 | zhangsan |
+------+----------+
2 rows in set (0.00 sec)

数据库的视图

  • 视图只是数据库中的虚拟表,不包含真实数据,只是做了一个映射

单表视图

  • 创建视图
mysql> create view v_score as select * from test1 where score>=80;
Query OK, 0 rows affected (0.00 sec)

  • 查看视图
mysql> select * from v_score;	//只有大于等于80的score记录
+------+----------+-------+------------+--------+
| id   | name     | score | address    | hobbid |
+------+----------+-------+------------+--------+
|    2 | lisi     | 90.00 | shengzheng |      2 |
|    4 | tianqi   | 99.00 | hangzhou   |      5 |
|    5 | wangdi   | 98.00 | laowo      |      3 |
|    3 | wangwu   | 80.00 | shanghai   |      4 |
|    1 | zhangsan | 80.00 | beijing    |      2 |
+------+----------+-------+------------+--------+
5 rows in set (0.00 sec)

  • 修改原表数据视图会变
mysql> update test1 set score='50' where name='wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from v_score;	//视图中没有wangwu的记录,因为改成了小于80的score了
+------+----------+-------+------------+--------+
| id   | name     | score | address    | hobbid |
+------+----------+-------+------------+--------+
|    2 | lisi     | 90.00 | shengzheng |      2 |
|    4 | tianqi   | 99.00 | hangzhou   |      5 |
|    5 | wangdi   | 98.00 | laowo      |      3 |
|    1 | zhangsan | 80.00 | beijing    |      2 |
+------+----------+-------+------------+--------+
4 rows in set (0.00 sec)

  • 通过视图修改原表
mysql> update v_score set score='100' where name='tianqi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from v_score;
+------+----------+--------+------------+--------+
| id   | name     | score  | address    | hobbid |
+------+----------+--------+------------+--------+
|    2 | lisi     |  90.00 | shengzheng |      2 |
|    4 | tianqi   | 100.00 | hangzhou   |      5 |
|    5 | wangdi   |  98.00 | laowo      |      3 |
|    1 | zhangsan |  80.00 | beijing    |      2 |
+------+----------+--------+------------+--------+
4 rows in set (0.00 sec)
mysql> select * from test1;
+------+-----------+--------+------------+--------+
| id   | name      | score  | address    | hobbid |
+------+-----------+--------+------------+--------+
|    6 | hanmeimei |  40.00 | nanjing    |      3 |
|    7 | lilei     |  50.00 | nanjing    |      5 |
|    2 | lisi      |  90.00 | shengzheng |      2 |
|    4 | tianqi    | 100.00 | hangzhou   |      5 |
|    5 | wangdi    |  98.00 | laowo      |      3 |
|    3 | wangwu    |  50.00 | shanghai   |      4 |
|    1 | zhangsan  |  80.00 | beijing    |      2 |
+------+-----------+--------+------------+--------+
7 rows in set (0.00 sec)

注:修改表不能修改以函数、复合函数方式计算出来的字段

多表视图

  • 先创建一个表,插入数据
mysql> create table test3 (id int,name varchar(10),age char(10));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into test3 values(1,'zhangsan',20);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test3 values(2,'lisi',30);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test3 values(3,'wangwu',29);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test3;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan | 20   |
|    2 | lisi     | 30   |
|    3 | wangwu   | 29   |
+------+----------+------+
3 rows in set (0.00 sec)

  • 创建一个视图,需要输出id、学生姓名、分数以及年龄
mysql> create view v_test1(id,name,score,age) as select test1.id,test1.name,test1.score,test3.age from test1,test3 where test1.name=test3.name;	//将各自表中需要的字段拿出来然后判断name相同,查询出来的结果就是name相同的
Query OK, 0 rows affected (0.00 sec)

mysql> select * from v_test1;
+------+----------+-------+------+
| id   | name     | score | age  |
+------+----------+-------+------+
|    2 | lisi     | 90.00 | 30   |
|    3 | wangwu   | 50.00 | 29   |
|    1 | zhangsan | 80.00 | 20   |
+------+----------+-------+------+
3 rows in set (0.00 sec)

null值

null值与空值的区别

  • NULL值的长度为null,占用空间
  • 空值长度为0,不占空间
  • 而函数计数会算入空值,忽略null值
mysql> alter table test1 add column addr varchar(50);	//增加一个字段
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> update test1 set addr='shanghai' where score >80;	//score大于80的addr定义为shanghai
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from test1;
+------+-----------+--------+------------+--------+----------+
| id   | name      | score  | address    | hobbid | addr     |
+------+-----------+--------+------------+--------+----------+
|    6 | hanmeimei |  40.00 | nanjing    |      3 | NULL     |
|    7 | lilei     |  50.00 | nanjing    |      5 | NULL     |
|    2 | lisi      |  90.00 | shengzheng |      2 | shanghai |
|    4 | tianqi    | 100.00 | hangzhou   |      5 | shanghai |
|    5 | wangdi    |  98.00 | laowo      |      3 | shanghai |
|    3 | wangwu    |  50.00 | shanghai   |      4 | NULL     |
|    1 | zhangsan  |  80.00 | beijing    |      2 | NULL     |
+------+-----------+--------+------------+--------+----------+
7 rows in set (0.00 sec)

mysql> select count(addr) from test1;	//函数只统计出三个值,也就是说null不算
+-------------+
| count(addr) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)

mysql> update test1 set addr='' where name='zhangsan';	//将zhangsanaddr改为空值
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test1;
+------+-----------+--------+------------+--------+----------+
| id   | name      | score  | address    | hobbid | addr     |
+------+-----------+--------+------------+--------+----------+
|    6 | hanmeimei |  40.00 | nanjing    |      3 | NULL     |
|    7 | lilei     |  50.00 | nanjing    |      5 | NULL     |
|    2 | lisi      |  90.00 | shengzheng |      2 | shanghai |
|    4 | tianqi    | 100.00 | hangzhou   |      5 | shanghai |
|    5 | wangdi    |  98.00 | laowo      |      3 | shanghai |
|    3 | wangwu    |  50.00 | shanghai   |      4 | NULL     |
|    1 | zhangsan  |  80.00 | beijing    |      2 |          |
+------+-----------+--------+------------+--------+----------+
7 rows in set (0.00 sec)

mysql> select count(addr) from test1;	//发现统计多一个,说明空值被算入统计中
+-------------+
| count(addr) |
+-------------+
|           4 |
+-------------+
1 row in set (0.01 sec)

  • 查询null值
mysql> select * from test1 where addr is null;
+------+-----------+-------+----------+--------+------+
| id   | name      | score | address  | hobbid | addr |
+------+-----------+-------+----------+--------+------+
|    6 | hanmeimei | 40.00 | nanjing  |      3 | NULL |
|    7 | lilei     | 50.00 | nanjing  |      5 | NULL |
|    3 | wangwu    | 50.00 | shanghai |      4 | NULL |
+------+-----------+-------+----------+--------+------+
3 rows in set (0.00 sec)

  • 查询不是null值的记录
mysql> select * from test1 where addr is not null;
+------+----------+--------+------------+--------+----------+
| id   | name     | score  | address    | hobbid | addr     |
+------+----------+--------+------------+--------+----------+
|    2 | lisi     |  90.00 | shengzheng |      2 | shanghai |
|    4 | tianqi   | 100.00 | hangzhou   |      5 | shanghai |
|    5 | wangdi   |  98.00 | laowo      |      3 | shanghai |
|    1 | zhangsan |  80.00 | beijing    |      2 |          |
+------+----------+--------+------------+--------+----------+
4 rows in set (0.00 sec)

你可能感兴趣的:(mysql)