目录
1.1.3 order by还可以结合where进行条件筛选过滤,筛选地址是杭州的学生按分数降序排序
6.2配合insert语句中,子查询的结果集可以通过 INSERT 语句插入到其 他的表中
2.7.6 需要创建一个视图,需要输入id、学生姓名、分数、以及分数
2.7.7这里可以修改原表数据来改变视图,也可以通过视图来修改原表
一、常见查询
1.1按关键字排序
使用 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,'liuyi',80,'beijing',2);
insert into info values(2,'wangwu',90,'shengzheng',2);
insert into info values(3,'lisi',60,'shanghai',4);
insert into info values(4,'tianqi',99,'hangzhou',5);
insert into info values(5,'jiaoshou',98,'laowo',3);
insert into info values(6,'hanmeimei',10,'nanjing',3);
insert into info values(7,'lilei',11,'nanjing',5);
+----+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+----+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
1.1.1 按分数排序,默认不指定是升序排序
mysql> select name,score from test1 order by score;
+-----------+-------+
| name | score |
+-----------+-------+
| hanmeimei | 10.00 |
| lilei | 11.00 |
| lisi | 60.00 |
| liuyi | 80.00 |
| wangwu | 90.00 |
| jiaoshou | 98.00 |
| tianqi | 99.00 |
+-----------+-------+
7 rows in set (0.00 sec)
1.1.2 按分数降序
mysql> select name,score from test1 order by score desc;
+-----------+-------+
| name | score |
+-----------+-------+
| tianqi | 99.00 |
| jiaoshou | 98.00 |
| wangwu | 90.00 |
| liuyi | 80.00 |
| lisi | 60.00 |
| lilei | 11.00 |
| hanmeimei | 10.00 |
+-----------+-------+
7 rows in set (0.00 sec)
1.1.3 order by还可以结合where进行条件筛选过滤,筛选地址是杭州的学生按分数降序排序
mysql> select name,score from test1 where address='hangzhou' order by score desc;
+--------+-------+
| name | score |
+--------+-------+
| tianqi | 99.00 |
+--------+-------+
1 row in set (0.00 sec)
1.1.4
ORDER BY 语句也可以使用多个字段来进行排序,当排序的第一个字段相同的记录有多条的情况下,这些多条的记录再按照第二个字段进行排序,ORDER BY 后面跟多个字段时,字段之间使用英文逗号隔开,优先级是按先后顺序而定
但order by 之后的第一个参数只有在出现相同值时,第二个字段才有意义
mysql> select name,score from test1 order by hobbid desc,score;
+-----------+-------+
| name | score |
+-----------+-------+
| lilei | 11.00 |
| tianqi | 99.00 |
| lisi | 60.00 |
| hanmeimei | 10.00 |
| jiaoshou | 98.00 |
| liuyi | 80.00 |
| wangwu | 90.00 |
+-----------+-------+
7 rows in set (0.00 sec)
1.2区间判断及查询不重复记录
1.2.1 and / or 和/或
mysql> select id,name,score from test1 where score>=60 and score <90;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | liuyi | 80.00 |
| 3 | lisi | 60.00 |
+----+-------+-------+
2 rows in set (0.00 sec)
1.2.2 嵌套/多条件
mysql> select id,name,score from test1 where score>=60 or(score >70 and score <100);
+----+----------+-------+
| id | name | score |
+----+----------+-------+
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 3 | lisi | 60.00 |
| 4 | tianqi | 99.00 |
| 5 | jiaoshou | 98.00 |
+----+----------+-------+
5 rows in set (0.00 sec)
1.2.3 distinct查询不记录
语法:
mysql> select distinct hobbid from test1;
+--------+
| hobbid |
+--------+
| 2 |
| 4 |
| 5 |
| 3 |
+--------+
4 rows in set (0.00 sec)
2.对结果进行分组
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),GROUP BY 分组的时候可以按一个或多个字段对结果进行分组处理。
2.1语法
SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator value GROUP BY column_name;
实例:
mysql> select count(name),score,hobbid from test1 group by hobbid,score;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
| 1 | 80.00 | 2 |
| 1 | 90.00 | 2 |
| 1 | 60.00 | 4 |
| 1 | 99.00 | 5 |
| 1 | 98.00 | 3 |
| 1 | 10.00 | 3 |
| 1 | 11.00 | 5 |
+-------------+-------+--------+
7 rows in set (0.00 sec)
2.2 结合where语句
mysql> select count(name),score,hobbid from test1 where score>=60 group by hobbid,score;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
| 1 | 80.00 | 2 |
| 1 | 90.00 | 2 |
| 1 | 60.00 | 4 |
| 1 | 99.00 | 5 |
| 1 | 98.00 | 3 |
+-------------+-------+--------+
5 rows in set (0.00 sec)
2.3 结合order by
mysql> select count(name),score,hobbid from test1 where score>=60 group by hobbid,score order by hobbid;
+-------------+-------+--------+
| count(name) | score | hobbid |
+-------------+-------+--------+
| 1 | 80.00 | 2 |
| 1 | 90.00 | 2 |
| 1 | 98.00 | 3 |
| 1 | 60.00 | 4 |
| 1 | 99.00 | 5 |
+-------------+-------+--------+
5 rows in set (0.00 sec)
3.限制结果条目
limit 限制输出的结果记录
在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录(行)。有时候仅 需要返回第一行或者前几行,这时候就需要用到 LIMIT 子句
3.1 语法
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number
示例:
mysql> select id,name,score,hobbid from test1 limit 3;
+----+--------+-------+--------+
| id | name | score | hobbid |
+----+--------+-------+--------+
| 1 | liuyi | 80.00 | 2 |
| 2 | wangwu | 90.00 | 2 |
| 3 | lisi | 60.00 | 4 |
+----+--------+-------+--------+
3 rows in set (0.00 sec)
3.1.2从第3行开始,往后显示2行内容
示例:
mysql> select id,name,score,hobbid from test1 limit 3,2;
+----+----------+-------+--------+
| id | name | score | hobbid |
+----+----------+-------+--------+
| 4 | tianqi | 99.00 | 5 |
| 5 | jiaoshou | 98.00 | 3 |
+----+----------+-------+--------+
2 rows in set (0.00 sec)
4.设置别名(alias ——》as)
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性
4.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 之后的别名,主要是为表内的列或者表提供临时的名称,在查询过程中使用,库内实际的表名 或字段名是不会被改变的
列别名设置实例1
mysql> select name as 姓名,score as 成绩 from test1;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| liuyi | 80.00 |
| wangwu | 90.00 |
| lisi | 60.00 |
| tianqi | 99.00 |
| jiaoshou | 98.00 |
| hanmeimei | 10.00 |
| lilei | 11.00 |
+-----------+--------+
7 rows in set (0.00 sec)
示例2
select i.name as 姓名,i.score as 成绩 from info as i;
查询表的字段数量(不带as也可以)
mysql> select count(*) as number from test1;
+--------+
| number |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
4.2
此外,AS 还可以作为连接语句的操作符。
创建t1表,将info表的查询记录全部插入t1表
mysql> create table info1 as select * from test1;
mysql> select * from info1; +----+-----------+-------+------------+--------+ | id | name | score | address | hobbid | +----+-----------+-------+------------+--------+ | 1 | liuyi | 80.00 | beijing | 2 | | 2 | wangwu | 90.00 | shengzheng | 2 | | 3 | lisi | 60.00 | shanghai | 4 | | 4 | tianqi | 99.00 | hangzhou | 5 | | 5 | jiaoshou | 98.00 | laowo | 3 | | 6 | hanmeimei | 10.00 | nanjing | 3 | | 7 | lilei | 11.00 | nanjing | 5 | +----+-----------+-------+------------+--------+ 7 rows in set (0.00 sec)
#此处AS起到的作用:
1、创建了一个新表t1 并定义表结构,插入表数据(与info表相同)
2、但是”约束“没有被完全”复制“过来 #但是如果原表设置了主键,那么附表的:default字段会默认设置一个0
5.通配符
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。
通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。常用的通配符有两个,分别是:
%:百分号表示零个、一个或多个字符 *
_:下划线表示单个字符
5.1查询以l开头的记录
mysql> select * from test1 where name like "l%";
+----+-------+-------+----------+--------+
| id | name | score | address | hobbid |
+----+-------+-------+----------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-------+-------+----------+--------+
3 rows in set (0.01 sec)
5.2查询开头是li,结尾是ei的记录
mysql> select * from test1 where name like "li_ei";
+----+-------+-------+---------+--------+
| id | name | score | address | hobbid |
+----+-------+-------+---------+--------+
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-------+-------+---------+--------+
1 row in set (0.00 sec)
6.子查询
子查询也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
格式:
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: 将主表和子表关联/连接的语法
准备一个表
mysql> create table ky30(id int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into ky30 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
多表查询
mysql> select id,name,score from test1 where id in (select id from ky30);
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 3 | lisi | 60.00 |
+----+--------+-------+
3 rows in set (0.00 sec)
6.1语法
<表达式> [NOT] IN <子查询>
当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。 若启用了 NOT 关键字,则返回值相反。需要注意的是,子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的
示例:
mysql> select id,name,score from test1 where id in (select id from ky30 where score>80);
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 2 | wangwu | 90.00 |
+----+--------+-------+
1 row in set (0.00 sec)
6.2配合insert语句中,子查询的结果集可以通过 INSERT 语句插入到其 他的表中
mysql> insert into test03 select * from test1 where id in (select id from test1);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from test03;
+------+-----------+-------+------------+--------+
| 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)
6.3 用update语句配合子循环
mysql> select * from test1;
+----+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+----+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
mysql> update test03 set score=50 where id in (select * from test1 where name='lisi');
ERROR 1241 (21000): Operand should contain 1 column(s)
mysql> update test03 set score=50 where id in (select id from test1 where name='lisi');
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test03;
+------+-----------+-------+------------+--------+
| 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 | 50.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)
6.4
在 IN 前面还可以添加 NOT,其作用与IN相反,表示否定(即不在子查询的结果集里面)
删除分数是大于等于70的记录
mysql> delete from test03 where id not in (select id where score <70);
Query OK, 4 rows affected (0.00 sec)
mysql> select * from test03;
+------+-----------+-------+----------+--------+
| id | name | score | address | hobbid |
+------+-----------+-------+----------+--------+
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 3 | lisi | 50.00 | shanghai | 4 |
+------+-----------+-------+----------+--------+
3 rows in set (0.00 sec)
6.5
EXISTS 这个关键字在子查询时,主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE
查询如果存在分数等于80的记录则计算info的字段数
示例:
mysql> select count(*) from test1 where exists(select * from test1 where score>0);
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
如果内函数内的条件不成立,那么就会是flase,直接返回结果0;
注意:
select * from 表名 此为标准格式,而以上的查询语句,"表名"的位置其实是一个完整结果集,mysql并不能直接识别,而此时给与结果集设置一个别名,以”select a.id from a“的方式查询将此结果集视为一张"表",就可以正常查询数据了,如下:
select a.id from (select id,name from info) a;
相当于
select info.id,name from info;
select 表.字段,字段 from 表;
二、Mysql视图
2.1概念
视图:优化操作+安全方案
数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射
视图可以理解为镜花水月/倒影,动态保存结果集(数据)
2.2作用场景
作用场景[图]:
针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示
2.3作用范围
select * from info; #展示的部分是info表
select * from view_name; #展示的一张或多张表
2.4功能
功能:
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性
本质而言视图是一种select(结果集的呈现)
2.5区别
①、视图是已经编译好的sql语句。而表不是
②、视图没有实际的物理记录。而表有。
show table status\G③、表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改
④、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。
⑤、表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。
⑥、视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的,就是update)
2.6他们之间的联系
视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系。
2.7视图的各种操作
2.7.1创建视图
mysql> create view v_test1 as select * from test1;
Query OK, 0 rows affected (0.01 sec)
2.7.2检查表的状态
mysql> show table status\G;
*************************** 6. row ***************************
Name: v_test1
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2024-09-04 08:14:38
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW
6 rows in set (0.02 sec)
2.7.3查看视图
mysql> select * from v_test1;
+----+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+----+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-----------+-------+------------+--------+
7 rows in set (0.01 sec)
2.7.4 查看视图与原表结构
mysql> desc v_test1;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int | NO | | NULL | |
| name | char(60) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | char(60) | YES | | NULL | |
| hobbid | int | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> desc test1;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int | NO | | NULL | |
| name | char(60) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | char(60) | YES | | NULL | |
| hobbid | int | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
提示:其实按道理test1应该是有主键的,但是没有配,视图不会有主键。
2.7.5
首先创建一个test01表
mysql> create table test04 (id int,name varchar(10),age char(10));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into test04 values(1,'zhangsan',20);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test04 values(2,'lisi',30);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test04 values(3,'wangwu',30);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test04;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 30 |
+------+----------+------+
3 rows in set (0.00 sec)
2.7.6 需要创建一个视图,需要输入id、学生姓名、分数、以及分数
mysql> create view v_test3 as select test1.id,test1.name,test1.score,test04.age from test04,test1 where teest1.name=test04.name;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from v_test3;
+----+--------+-------+------+
| id | name | score | age |
+----+--------+-------+------+
| 2 | wangwu | 90.00 | 29 |
| 3 | lisi | 60.00 | 30 |
+----+--------+-------+------+
2 rows in set (0.01 sec)
mysql> select * from test04;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
+------+----------+------+
3 rows in set (0.00 sec)
mysql> select * from test1;
+----+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+----+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-----------+-------+------------+--------+
7 rows in set (0.01 sec)
mysql>
注意:这里发现语句中创建的视图名称后面不用加字段名称。
2.7.7这里可以修改原表数据来改变视图,也可以通过视图来修改原表
(1)修改原表数据
原表
mysql> select * from test04;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 30 |
+------+----------+------+
修改原表
mysql> update test04 set age=29 where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看修改过后的视图
mysql> select * from V_test3;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 20 |
| 2 | lisi | 30 |
| 3 | wangwu | 29 |
(2)通过视图修改原表
原表
mysql> update v_test1 set score=88 where name='lisi';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from v_test1;
+----+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+----+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 88.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
mysql> select * from test1;
+----+-----------+-------+------------+--------+
| id | name | score | address | hobbid |
+----+-----------+-------+------------+--------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 88.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
+----+-----------+-------+------------+--------+
7 rows in set (0.00 sec)
2.8 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(3) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | UNI | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(50) | YES | | 未知 | |
| hobbid | int(3) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
插入一条记录,分数字段输入null,显示出来就是null
#验证:
alter table info add column addr varchar(50);
update info set addr='nj' where score >=70;
#统计数量:检测null是否会加入统计中
select count(addr) from info;
#将info表中其中一条数据修改为空值''
update info set addr='' where name='wangwu';
#统计数量,检测空值是不会被添加到统计中
select count(addr) from info;
#查询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 |
+------+-----------+-------+---------+--------+------+
2 rows in set (0.00 sec)
#查询不为空的值
ysql> select * from info where addr is not null;
+------+----------+-------+------------+--------+------+
| id | name | score | address | hobbid | addr |
+------+----------+-------+------------+--------+------+
| 1 | liuyi | 80.00 | beijing | 2 | nj |
| 2 | wangwu | 90.00 | shengzheng | 2 | nj |
| 3 | lisi | 60.00 | shanghai | 4 | |
| 4 | tianqi | 99.00 | hangzhou | 5 | nj |
| 5 | jiaoshou | 98.00 | laowo | 3 | nj |
| 1 | xiaoer | 80.00 | hangzhou | 3 | nj |
+------+----------+-------+------------+--------+------+
6 rows in set (0.00 sec)
三。连接查询
这里准备的模版
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);
3.1内连接
MySQL 中的内连接就是两张或多张表中同时符合某种条件的数据记录的组合。通常在 FROM 子句中使用关键字 INNER JOIN 来连接多张表,并使用 ON 子句设置连接条件,内连接是系统默认的表连接,所以在 FROM 子句后可以省略 INNER 关键字,只使用 关键字 JOIN。同时有多个表时,也可以连续使用 INNER JOIN 来实现多表的内连接,不过为了更好的性能,建议最好不要超过三个表
示例:
SELECT column_name(s)FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
模板表:
create table infos(name varchar(40),score decimal(4,2),address varchar(40));
insert into infos values('wangwu',80,'beijing'),('zhangsan',99,'shanghai'),('lisi',100,'nanjing');
mysql> select * from infos;
+----------+-------+----------+
| name | score | address |
+----------+-------+----------+
| wangwu | 80.00 | beijing |
| zhangsan | 99.00 | shanghai |
| lisi | 99.99 | nanjing |
+----------+-------+----------+
mysql> select info.id,info.name from info inner join infos on info.name=infos.name;
+------+--------+
| id | name |
+------+--------+
| 2 | wangwu |
| 3 | lisi |
+------+--------+
2 rows in set (0.00 sec)
对两个表做内连接
mysql> select test1.id,test1.name from test1 inner join infos on test1.name=infos.name;
+----+--------+
| id | name |
+----+--------+
| 2 | wangwu |
| 3 | lisi |
+----+--------+
2 rows in set (0.00 sec)
3.2左链接
左连接也可以被称为左外连接,在 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 | 50.00 | shengzheng | 2 | nj | wangwu | 80.00 | beijing |
| 3 | lisi | 50.00 | shanghai | 4 | nj | lisi | 99.99 | nanjing |
| 1 | liuyi | 60.00 | beijing | 2 | nj | NULL | NULL | NULL |
| 4 | tianqi | 100.00 | hangzhou | 5 | | NULL | NULL | NULL |
| 5 | jiaoshou | 100.00 | laowo | 3 | NULL | NULL | NULL | NULL |
| 6 | hanmeimei | 100.00 | nanjing | 3 | NULL | NULL | NULL | NULL |
| 7 | lilei | 100.00 | nanjing | 5 | NULL | NULL | NULL | NULL |
| 7 | lilei | 100.00 | nanjing | 5 | NULL | NULL | NULL | NULL |
| 8 | abn | 81.00 | bj | 1 | nj | NULL | NULL | NULL |
+------+-----------+--------+------------+--------+------+--------+-------+---------+
左连接中左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录,右表记录不足的地方均为 NULL。
3.3右连接
右连接也被称为右外连接,在 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 | 50.00 | shengzheng | 2 | nj | wangwu | 80.00 | beijing |
| NULL | NULL | NULL | NULL | NULL | NULL | zhangsan | 99.00 | shanghai |
| 3 | lisi | 50.00 | shanghai | 4 | nj | lisi | 99.99 | nanjing |
+------+--------+-------+------------+--------+------+----------+-------+----------+
3 rows in set (0.00 sec)
在右连接的查询结果集中,除了符合匹配规则的行外,还包括右表中有但是左表中不匹 配的行,这些记录在左表中以 NULL 补足