高阶SQL语句(二)

一 子查询

也被称作内查询或者嵌套查询,是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句

是先于主查询语句被执行的,其结果作为外层的条件返回给主查询进行下一 步的查询过滤。

①子语句可以与主语句所查询的表相同,也可以是不同表

②子语句中的sql语句是为了,最后过滤出一个结果集,用于主语句的判断条件

③ in: 将主表和子表关联/连接的语法

环境准备:

mysql> use kgc_ky35;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+--------------------+
| Tables_in_kgc_ky35 |
+--------------------+
| HP                 |
| test01             |
| test02             |
| test03             |
+--------------------+
4 rows in set (0.00 sec)mysql> select * from HP;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
in:查询已知数据记录

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


mysql> create table football (id int);
Query OK, 0 rows affected (0.01 sec)mysql> insert into football values(1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select * from football;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)mysql> select id,name,score from HP where id in (select id from football);
+------+------+-------+
| id   | name | score |
+------+------+-------+
|    1 | hz   | 90.00 |
|    2 | mdq  | 80.00 |
|    2 | cx   | 60.00 |
+------+------+-------+
3 rows in set (0.00 sec)#先查询football数据表中的id字段列  将查询到的结果id字段列作为一个已知的值的数据记录;再根据已知的值的数据记录查询football数据表中id,name,score字段列
多表查询举例:
mysql> select name,score from HP where id in (select id from football where score>60);
+------+-------+
| name | score |
+------+-------+
| hz   | 90.00 |
| mdq  | 80.00 |
+------+-------+
2 rows in set (0.00 sec)
mysql> select name,score from HP where id in (select id from HP where score>70);
+------+-------+
| name | score |
+------+-------+
| hz   | 90.00 |
| mdq  | 80.00 |
| cx   | 60.00 |
+------+-------+
3 rows in set (0.00 sec)

多表查询

子查询不仅可以在 SELECT 语句中使用,在 INERT、UPDATE、DELETE 中也同样适用。在嵌套

的时候,子查询内部还可以再次嵌套新的子查询,也就是说可以多层嵌套。

①语法

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

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

当表达式与子查询返回的结果集中的某个值相等时,返回 TRUE,否则返回 FALSE。 若启用了 NOT 关键字,则返回值相反。

注意:子查询只能返回一列数据,如果需 求比较复杂,一列解决不了问题,可以使用多层嵌套的

方式来应对。 多数情况下,子查询都是与 SELECT 语句一起使用的

表示 先匹配出member表内的id字段为基础匹配的结果集(2,3),然后再执行主语句,以主语句的id 为基础 进行where 条件判断/过滤

②插入
mysql> select * from test03;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
+------+------+-------+---------+-------+
2 rows in set (0.00 sec)mysql> delete from test03;
Query OK, 2 rows affected (0.01 sec)mysql> insert into test03 select * from test01 where id in ( select id from HP);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select * from test03;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
③修改数据:

update语句也可以使用子查询。update 内的子查询,在 set 更新内容时,可以是单独的一

列,也可以是多列。

mysql> select * from test03;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> update test03 set score=44 where id in (select id from HP where id=1);
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from test03;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 44.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
④删除:

在 IN 前面还可以添加 NOT,其作用与IN相反,表示否定(即不在子查询的结果集里面)

DELETE 也适用于子查询,

环境准备:

mysql> show tables;
+--------------------+
| Tables_in_kgc_ky35 |
+--------------------+
| HP                 |
| football           |
| jinjin             |
| kangkang           |
| test01             |
| test02             |
| test03             |
| test1              |
| test2              |
| v_HP               |
| v_hc               |
| v_score            |
+--------------------+
12 rows in set (0.00 sec)mysql> select * from test02;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
mysql> delete from test02 where id in (select id from test02 where score > 85);
ERROR 1093 (HY000): You can't specify target table 'test02' for update in FROM clause
mysql> select * from test02 where score > 85;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
+------+------+-------+---------+-------+
1 row in set (0.00 sec)mysql> delete from test02 where id in (select id from test02 where score > 85);
ERROR 1093 (HY000): You can't specify target table 'test02' for update in FROM clause
mysql> select * from test01;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)多表删除,至少两个表,且有相同的部分才行mysql> update test01 set score=96,hobby=2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from test01;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 96.00 | tianye  |     2 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.01 sec)mysql> delete from test02 where id in (select id from test01 where score > 85);
Query OK, 1 row affected (0.01 sec)

结果:

mysql> select * from test01;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 96.00 | tianye  |     2 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> select * from test02;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
2 rows in set (0.00 sec)
⑤exists 

主要用于判断子查询的结果集是否为空。如果不为空, 则返回 TRUE;反之,则返回 FALSE;

mysql> select * from test02;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> select sum(score) from test02 where exists(select id from test02 where score <80);
+------------+
| sum(score) |
+------------+
|     230.00 |
+------------+
1 row in set (0.00 sec)mysql> select sum(score) from test02 where exists(select id from test02 where score >90);
+------------+
| sum(score) |
+------------+
|       NULL |
+------------+
1 row in set (0.00 sec)
⑥not取反

在 IN 前面还可以添加 NOT,其作用与IN相反,表示否定(即不在子查询的结果集里面)

mysql> select * from test01;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 96.00 | tianye  |     2 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    3 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> select * from test02;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    4 | jhg  | 85.00 | nanjing |     4 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    3 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from test01 where id not  in(select id  from test02 where id);
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 96.00 | tianye  |     2 |
+------+------+-------+---------+-------+
1 row in set (0.00 sec)
⑦别名as

查询info表id,name 字段;select id,name from info;
可以查看到info表的内容

将结果集做为一张表进行查询的时候,我们也需要用到别名,示例:

需求:从info表中的id和name字段的内容做为"内容" 输出id的部分

mysql> select * from test02;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> select id ,name from HP;
+------+------+
| id   | name |
+------+------+
|    1 | hz   |
|    2 | mdq  |
|    2 | cx   |
+------+------+
3 rows in set (0.00 sec)mysql> select id ,name from (select id,name from HP) a;
+------+------+
| id   | name |
+------+------+
|    1 | hz   |
|    2 | mdq  |
|    2 | cx   |
+------+------+
3 rows in set (0.01 sec)

二 视图=优化操作+安全方案  

数据库中的虚拟表,这张虚拟表中不包含真实数据,只是做了真实数据的映射
视图可以理解为镜花水月/倒影,动态保存结果集(数据)

作用场景:

针对不同的人(权限身份),提供不同结果集的“表”(以表格的形式展示)

展示的部分是info表

展示的一张或多张表

功能:

简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性

本质而言视图是一种select(结果集的呈现)

视图适合于多表连接浏览时使用!不适合增、删、改

而存储过程适合于使用较频繁的SQL语句,这样可以提高执行效率!

视图和表的区别

① 视图是已经编译好的sql语句。而表不是

② 视图没有实际的物理记录。而表有。
show table status\G

③ 表只用物理空间而视图不占用物理空间,视图只是逻辑概念的存在,表可以及时对它进行修改,但视图只能有创建的语句来修改

④ 视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表结构。

⑤ 表属于全局模式中的表,是实表;视图属于局部模式的表,是虚表。

⑥ 视图的建立和删除只影响视图本身,不影响对应的基本表。(但是更新视图数据,是会影响到基本表的)

联系:

视图(view)是在基本表之上建立的表,它的结构(即所定义的列)和内容(即所有数据行)都来自基本

表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基

本表的抽象和在逻辑意义上建立的新关系。

单表演练

mysql> desc test02;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(10)      | YES  |     | NULL    |       |
| name    | varchar(16)  | NO   |     | NULL    |       |
| score   | decimal(5,2) | YES  |     | NULL    |       |
| address | varchar(40)  | YES  |     | NULL    |       |
| hobby   | int(8)       | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)mysql> create view v_score as select * from test02 where score>=90;
Query OK, 0 rows affected (0.01 sec)
#创建视图mysql> show table status\G     #查看表状态
*************************** 1. row ***************************Name: HPEngine: MyISAMVersion: 10Row_format: DynamicRows: 3Avg_row_length: 28Data_length: 84
Max_data_length: 281474976710655Index_length: 2048Data_free: 0Auto_increment: NULLCreate_time: 2024-03-26 15:53:59Update_time: 2024-03-26 15:58:45Check_time: NULLCollation: utf8_general_ciChecksum: NULLCreate_options: Comment: 
*************************** 2. row ***************************
mysql> desc v_score;   #查看视图与源表结构
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(10)      | YES  |     | NULL    |       |
| name    | varchar(16)  | NO   |     | NULL    |       |
| score   | decimal(5,2) | YES  |     | NULL    |       |
| address | varchar(40)  | YES  |     | NULL    |       |
| hobby   | int(8)       | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

多表视图创立

相同的名字或相同的值都可以导入里面

mysql> create table kangkang(id int,name varchar(15),age varchar(16));
Query OK, 0 rows affected (0.00 sec)mysql> select * from HP;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> insert into kangkang values(1,'liubei',56);
Query OK, 1 row affected (0.00 sec)mysql> insert into kangkang values(2,'lubu',40);
Query OK, 1 row affected (0.00 sec)mysql> insert into kangkang values(3,'jiaxu',75);
Query OK, 1 row affected (0.00 sec)mysql> insert into kangkang values(4,'mazhong',28);
Query OK, 1 row affected (0.01 sec)mysql> select * from kangkang;
+------+---------+------+
| id   | name    | age  |
+------+---------+------+
|    1 | liubei  | 56   |
|    2 | lubu    | 40   |
|    3 | jiaxu   | 75   |
|    4 | mazhong | 28   |
+------+---------+------+
4 rows in set (0.00 sec)
4 rows in set (0.00 sec)mysql> create view v_HP(id,name,score,age) as select k.id,k.name,k.score,s.age from HP k,kangkang s where k.name=s.name;
Query OK, 0 rows affected (0.01 sec)mysql> select * from v_HP;
Empty set (0.01 sec)
#因为名字不相同,需要改一下,
mysql> create view v_hc(id,name,score,age) as select k.id,k.name,k.score,s.age from HP k,kangkang s where k.name=s.name;
Query OK, 0 rows affected (0.01 sec)
# v_hc,可任意取名,
mysql> select * from v_hc;
+------+------+-------+------+
| id   | name | score | age  |
+------+------+-------+------+
|    2 | cx   | 60.00 | 28   |
+------+------+-------+------+
修改表数据

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

mysql> select * from HP;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
mysql> update HP set score='77' where name='cx';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from v_HP;
+------+------+-------+------+
| id   | name | score | age  |
+------+------+-------+------+
|    2 | cx   | 77.00 | 28   |
+------+------+-------+------+
修改视图

修改视图时也修改表的数据

mysql> select * from v_HP;
+------+------+-------+------+
| id   | name | score | age  |
+------+------+-------+------+
|    2 | cx   | 77.00 | 28   |
+------+------+-------+------+
1 row in set (0.00 sec)mysql> update v_HP set score='98' where name='cx';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from v_HP;
+------+------+-------+------+
| id   | name | score | age  |
+------+------+-------+------+
|    2 | cx   | 98.00 | 28   |
+------+------+-------+------+
1 row in set (0.00 sec)mysql> select * from HP;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 85.00 | renduo  |     3 |
|    2 | cx   | 98.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)

三 NULL 值--缺失

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

null值与空值的区别(空气与真空)

空值长度为0,不占空间,NULL值的长度为null,占用空间

is null无法判断空值;空值使用"=“或者”<>"来处理(!=)

count()计算时,NULL会忽略,空值会加入计算
一般三种模式:

mysql> select length(null),length(''),length('ad');
+--------------+------------+--------------+
| length(null) | length('') | length('ad') |
+--------------+------------+--------------+
|         NULL |          0 |            2 |
+--------------+------------+--------------+
1 row in set (0.00 sec)
mysql> show tables;
+--------------------+
| Tables_in_kgc_ky35 |
+--------------------+
| HP                 |
| football           |
| jinjin             |
| kangkang           |
| test01             |
| test02             |
| test03             |
| test1              |
| test2              |
| v_HP               |
| v_hc               |
| v_score            |
+--------------------+
12 rows in set (0.00 sec)mysql> select * from HP;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 85.00 | renduo  |     3 |
|    2 | cx   | 98.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> alter table HP add length varchar(18);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select * from HP;
+------+------+-------+---------+-------+--------+
| id   | name | score | address | hobby | length |
+------+------+-------+---------+-------+--------+
|    1 | hz   | 90.00 | tianye  |     3 | NULL   |
|    2 | mdq  | 85.00 | renduo  |     3 | NULL   |
|    2 | cx   | 98.00 | guancai |     3 | NULL   |
+------+------+-------+---------+-------+--------+
3 rows in set (0.00 sec)
mysql> select * from HP where length is null;
+------+------+-------+---------+-------+--------+
| id   | name | score | address | hobby | length |
+------+------+-------+---------+-------+--------+
|    1 | hz   | 90.00 | tianye  |     3 | NULL   |
|    2 | mdq  | 85.00 | renduo  |     3 | NULL   |
|    2 | cx   | 98.00 | guancai |     3 | NULL   |
+------+------+-------+---------+-------+--------+
3 rows in set (0.00 sec)mysql> select count(length) from HP;
+---------------+
| count(length) |
+---------------+
|             0 |
+---------------+
1 row in set (0.00 sec)

四 连接查询

将来自两个或多个表的记录行结合起来,基于这些表之间的 共同字段,进行数据的查询。

尽量两个表查询,三个表会延缓显示时间 

  • 内连接
  • 左连接
  • 右连接

环境准备:

mysql> select * from test02;
+------+------+-------+---------+-------+
| id   | name | score | address | hobby |
+------+------+-------+---------+-------+
|    1 | hz   | 90.00 | tianye  |     3 |
|    2 | mdq  | 80.00 | renduo  |     3 |
|    2 | cx   | 60.00 | guancai |     3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)mysql> select * from kangkang;
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    1 | liubei | 56   |
|    2 | lubu   | 40   |
|    3 | jiaxu  | 75   |
|    4 | cx     | 28   |
+------+--------+------+
4 rows in set (0.00 sec)

①内连接

就是两张或多张表中同时符合某种条件的数据记录的组合。通常在 FROM 子句中使用关键字 INNER JOIN 来连接多张表,并使用 ON 子句设置连接条件,内连接是系统默认的表连接,所以在 FROM 子句后可以省略 INNER 关键字,只使用 关键字 JOIN。同时有多个表时,也可以连续使用 INNER JOIN 来实现多表的内连接,

注意:不过为了更好的性能,建议最好不要超过三个表

格式:

select 表1[2].字段1,表1[2].字段2,... from 表1 inner join 表2 on 表1.同名字段=表2.同名字段;
mysql> select test02.id,test02.name from test02 inner join kangkang on test02.name=kangkang.name;
+------+------+
| id   | name |
+------+------+
|    2 | cx   |
+------+------+
1 row in set (0.00 sec)

内连查询:通过inner join 的方式将两张表指定的相同字段的记录行输出出来

②左连接

在 FROM 子句中使用 LEFT JOIN 或者 LEFT OUTER JOIN 关键字来表示。左连接以左侧表为基础表,接收左表的所有行,并用这些行与右侧参 考表中的记录进行匹配,也就是说匹配左表中的所有行以及右表中符合条件的行

格式:

select * from 表1 left join 表2 on 表1.同名字段=表2.同名字段;

左连接中左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录,右表记录不足的地方均为 NULL。

mysql> select * from test02 left join kangkang on test02.name=kangkang.name;
+------+------+-------+---------+-------+------+------+------+
| id   | name | score | address | hobby | id   | name | age  |
+------+------+-------+---------+-------+------+------+------+
|    2 | cx   | 60.00 | guancai |     3 |    4 | cx   | 28   |
|    1 | hz   | 90.00 | tianye  |     3 | NULL | NULL | NULL |
|    2 | mdq  | 80.00 | renduo  |     3 | NULL | NULL | NULL |
+------+------+-------+---------+-------+------+------+------+
3 rows in set (0.00 sec)

③右连接

在 FROM 子句中使用 RIGHT JOIN 或者 RIGHT OUTER JOIN 关键字来表示。右连接跟左连接正好相反,它是以右表为基础表,用于接收右表中的所有行,并用这些记录与左表中的行进行匹配

mysql> select * from test02 right join kangkang on test02.name=kangkang.name;
+------+------+-------+---------+-------+------+--------+------+
| id   | name | score | address | hobby | id   | name   | age  |
+------+------+-------+---------+-------+------+--------+------+
|    2 | cx   | 60.00 | guancai |     3 |    4 | cx     | 28   |
| NULL | NULL |  NULL | NULL    |  NULL |    1 | liubei | 56   |
| NULL | NULL |  NULL | NULL    |  NULL |    2 | lubu   | 40   |
| NULL | NULL |  NULL | NULL    |  NULL |    3 | jiaxu  | 75   |
+------+------+-------+---------+-------+------+--------+------+
4 rows in set (0.00 sec)

在右连接的查询结果集中,除了符合匹配规则的行外,还包括右表中有但是左表中不匹 配的行,这些记录在左表中以 NULL 补足

五 存储过程

前面学习的 MySQL 相关知识都是针对一个表或几个表的单条 SQL 语句,使用这样的SQL 语句虽

然可以完成用户的需求,但在实际的数据库应用中,

有些数据库操作可能会非常复杂,可能会需要多条 SQL 语句一起去处理才能够完成,这时候就可

以使用存储过程, 轻松而高效的去完成这个需求,有点类似shell脚本里的函数

1 存储过程是一组为了完成特定功能的SQL语句集合。  两个点 第一 触发器(定时任务) 第二个判断 
2 存储过程这个功能是从5.0版本才开始支持的,它可以加快数据库的处理速度,增强数据库在实际应用中的灵活性。存储过程在使用过程中是将常用或者复杂的工作预先使用SQL语句写好并用一个指定的名称存储起来,这个过程经编译和优化后存储在数据库服务器中。当需要使用该存储过程时,只需要调用它即可。操作数据库的传统 SQL 语句在执行时需要先编译,然后再去执行,跟存储过程一对比,明显存储过程在执行上速度更快,效率更高

1 存储过程的优点:

①执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率

②SQL语句加上控制语句的集合,灵活性高

③在服务器端存储,客户端调用时,降低网络负载

④可多次重复被调用,可随时修改,不影响客户端调用

⑤可完成所有的数据库操作,也可控制数据库的信息访问权限

语法:

CREATE PROCEDURE <过程名> ( [过程参数[,…] ] ) <过程体>
[过程参数[,…] ] 格式
<过程名>:尽量避免与内置的函数或字段重名
<过程体>:语句
[ IN | OUT | INOUT ] <参数名><类型>
mysql> delimiter $$
#将语句的结束符号从分号;临时改为两个$$(可以自定义)
mysql> create procedure class()
#创建存储过程,过程名为class,不带参数-> begin
#过程体以关键字 BEGIN 开始-> create table class3(id int,name varchar(8),score decimal(5,2));-> insert into class3 values(1,'wsc',98),(2,'ljc',95);-> select * from class3;
#过程体语句-> END $$
#过程体以关键字 END 结束
Query OK, 0 rows affected (0.12 sec)
mysql> DELIMITER ;
#将语句的结束符号恢复为分号

环境准备:

mysql> show tables;
+--------------------+
| Tables_in_kgc_ky35 |
+--------------------+
| HP                 |
| football           |
| kangkang           |
| test01             |
| test02             |
| test03             |
| test1              |
| test2              |
| v_HP               |
| v_hc               |
| v_score            |
+--------------------+
11 rows in set (0.00 sec)mysql> select * from kangkang;
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    1 | liubei | 56   |
|    2 | lubu   | 40   |
|    3 | jiaxu  | 75   |
|    4 | cx     | 28   |
+------+--------+------+
4 rows in set (0.00 sec)

创建存储

mysql> delimiter $$
mysql> create procedure kangkang ()-> begin-> create table jinjin(id int,name varchar(10),score int(20));-> insert into jinjin values(1,'dieyi',65);-> select * from jinjin;-> END $$
Query OK, 0 rows affected (0.00 sec)

2 调用存储


mysql> call kangkang;
+------+-------+-------+
| id   | name  | score |
+------+-------+-------+
|    1 | dieyi |    65 |
+------+-------+-------+
1 row in set (0.01 sec)Query OK, 0 rows affected (0.01 sec)

3 查看存储

mysql> show create procedure kangkang\G
*************************** 1. row ***************************Procedure: kangkangsql_mode: PIPES_AS_CONCAT,ANSI_QUOTES,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIONCreate Procedure: CREATE DEFINER="root"@"localhost" PROCEDURE "kangkang"()
begin
create table jinjin(id int,name varchar(10),score int(20));
insert into jinjin values(1,'dieyi',65);
select * from jinjin;
END
character_set_client: utf8
collation_connection: utf8_general_ciDatabase Collation: utf8_general_ci
1 row in set (0.00 sec)
mysql> show  procedure status like '%kangkang%'\G
*************************** 1. row ***************************Db: kgc_ky35Name: kangkangType: PROCEDUREDefiner: root@localhostModified: 2024-03-27 19:46:16Created: 2024-03-27 19:46:16Security_type: DEFINERComment: 
character_set_client: utf8
collation_connection: utf8_general_ciDatabase Collation: utf8_general_ci
1 row in set (0.00 sec)

4 存储过程参数

IN 输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)

OUT 输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)

INOUT 输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)

即表示调用者向过程传入值,又表示过程向调用者传出值(只能是变量)

mysql>  delimiter @@
mysql> create procedure yinyin(in inname varchar(40))-> begin-> select * from kangkang where name=inname;-> end @@
Query OK, 0 rows affected (0.01 sec)mysql> call yinyin('cx');
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | cx   | 28   |
+------+------+------+
1 row in set (0.00 sec)

5 修改存储

ALTER PROCEDURE <过程名>[<特征>... ]
SECURITY:安全等级
invoker:当定义为INVOKER时,只要执行者有执行权限,就可以成功执行。

6 删除存储

 存储过程内容的修改方法是通过删除原有存储过程,之后再以相同的名称创建新的存储过程。

mysql> drop procedure if exists cx;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> call cx();
ERROR 1305 (42000): PROCEDURE kgc_ky35.cx does not exist
mysql> show create procedure cx\G;
ERROR 1305 (42000): PROCEDURE cx does not exist
ERROR: 
No query specified

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/774976.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32收发HEX数据包

在实际应用中&#xff0c;STM32的串口通信都是以数据包格式进行收发&#xff0c;这个数据包一般都包含包头和包尾&#xff0c;表示一个数据包。源代码在文末给出 数据包格式&#xff1a; 固定长度&#xff0c;含包头包尾 可变包长&#xff0c;含包头包尾 问题1&#xff1a;当…

YoloV5改进策略:BackBone改进|ECA-Net:用于深度卷积神经网络的高效通道注意力

摘要 本文使用ECA-Net注意力机制加入到YoloV5中。我尝试了多种改进方法&#xff0c;并附上改进结果&#xff0c;方便大家了解改进后的效果&#xff0c;为论文改进提供思路。&#xff08;更新中。。。。&#xff09; 论文&#xff1a;《ECA-Net&#xff1a;用于深度卷积神经网…

183. 从不订购的客户

文章目录 题意思路代码 题意 题目链接 查找未出现在orders表里面的内容 思路 子查询not in 代码 select name as Customers from Customers where id not in (select customerId from Orders group by customerId)

86.分隔链表

给你一个链表的头节点 head 和一个特定值 x &#xff0c;请你对链表进行分隔&#xff0c;使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。 你应当 保留 两个分区中每个节点的初始相对位置。 示例 1&#xff1a; ​ 输入&#xff1a;head [1,4,3,2,5,2], x 3 输出&…

前端学习之JavaScript有关字符串的一些方法

&#xff08;注释是对各个方法的一些解释&#xff09; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>字符串</title> </head> <body><script>let str1 test1let str2 n…

前端性能优化:掌握解决方案

课程介绍 我们常说性能永远是第一需求&#xff0c;作为一个前端工程师&#xff0c;不管使用什么框架&#xff0c;不管从事什么类型的网站或应用开发&#xff0c;只要是项目被用户使用&#xff0c;性能优化就永远是你需要关注的问题。通常情况下&#xff0c;工程师们在深入了解…

[C++打怪升级]--学习总目录

总结C打怪升级学习目录&#xff0c;便于翻阅&#xff0c;制作不易&#xff0c;点个赞吧&#xff0c;感谢&#xff01; 基础入门 ​​​​​​[C基础入门]&#xff08;一&#xff09;&#xff1a;初识 [C基础入门]&#xff08;二&#xff09;&#xff1a;数据类型 [C基础入门…

esp32c6 micropython固件首发

挺久没写正经文章了&#xff0c;主要是micropython确实也没那么多可挖掘的东西&#xff0c;这次带来的是micropython esp32c6 抢先版的固件&#xff0c;是df论坛的一位大佬编译的&#xff0c;属于测试阶段 固件下载地址 我30岁开始学编程&#xff0c;现在33了&#xff0c;终于程…

白板手推公式性质 AR模型 时间序列分析

白板手推公式性质 AR模型 时间序列分析 视频讲解&#xff1a;https://www.bilibili.com/video/BV1D1421S76v/?spm_id_from.dynamic.content.click&vd_source6e452cd7908a2d9b382932f345476fd1 B站对应视频讲解(白板手推公式性质 AR模型 时间序列分析)

[C语言]带连接数统计功能的多进程TCP服务器

编程思想: so,我们一分钱没花改造了一个简易TCP服务器,具体的: 1 当accept正常返回后,创建一个子进程用于处理数据 2 在子进程中 关闭socket返回的fd,在父进程中关闭accept返回的fd,防止资源泄露及不可预知的风险 3 父进程中忽略子进程结束信号,等于自动回收,防止变僵尸 当…

hdlbits系列verilog解答(Hadd)-65

文章目录 一、问题描述二、verilog源码三、仿真结果一、问题描述 本节我们创建一个半加法器。半加法器将两个位相加(无进位)并产生求和和进出。 模块声明 module top_module( input a, b, output cout, sum ); 思路: 可用真值表写出逻辑表达式,或者直接用数据流方式。 二…

Qt 压缩/解压文件

前面讲了很多Qt的文件操作&#xff0c;文件操作自然就包括压缩与解压缩文件了&#xff0c;正好最近项目里要用到压缩以及解压缩文件&#xff0c;所以就研究了一下Qt如何压缩与解压缩文件。 QZipReader/QZipWriter QZipReader 和 QZipWriter 类提供了用于读取和写入 ZIP 格式文…

linux 多个文件(csv)合并成一个文件(csv)

文章目录 前言实例:实战:另外&#xff0c;补充一个相关知识 总结 前言 Linux之cat合并多个文件 实例: # 将当前目录下所有csv结尾的文件合并到merge.csv cat *.csv > merge.csv # 当然也可以指定合并哪几个文件 cat db1.sql db2.sql db3.sql > db_all.sql 实战: 将每个…

好物视频素材哪里找?推荐以下几个自用很好的视频素材库

好物视频素材哪里找&#xff1f;这可是个让很多创作者头疼的问题。想制作一个吸引人的视频&#xff0c;好的素材可是关键。下面就给大家介绍几个热门的视频素材网站&#xff0c;希望能帮到你&#xff01; 蛙学网&#xff08;https://www.waxuewang.com/&#xff09;&#xff1…

国产数据库序列机制

数据库 达梦 序列&#xff1a;支持 主键自增&#xff1a;支持 使用序列 //1.创建序列 create sequence <序列名> increment by 10...; //2.使用序列&#xff0c;插入时指定&#xff0c;或者设计表字段默认值为seq1.nextval insert into <表名>(id,...) values…

netty构建udp服务器以及发送报文到客户端客户端详细案例

目录 一、基于netty创建udp服务端以及对应通道设置关键 二、发送数据 三、netty中的ChannelOption常用参数说明 1、ChannelOption.SO_BACKLOG 2、ChannelOption.SO_REUSEADDR 3、ChannelOption.SO_KEEPALIVE 4、ChannelOption.SO_SNDBUF和ChannelOption.SO_RCVBUF 5、Ch…

vs code

vs code 下载安装 https://code.visualstudio.com/https://code.visualstudio.com/ 下载完后&#xff0c;下一步下一步就安装完了&#xff0c;安装好后可以下载各种好用的插件

无需 VPN 即可急速下载 huggingface 上的 LLM 模型

无需 VPN 即可急速下载 huggingface 上的 LLM 模型 无需 VPN 即可急速下载 huggingface 上的 llm 模型安装依赖配置下载命令 无需 VPN 即可急速下载 huggingface 上的 llm 模型 快速下载huggingface模型&#xff1a; 安装依赖 pip install -U huggingface_hub hf_transfer …

已注册的商标别忘了续展,新注可能难下证!

近期普推知产老杨遇到好几个网友和看过多个案例&#xff0c;以前商标名称可以申请注册下来&#xff0c;但是换字体注册不下来了&#xff0c;有的是不想续展想直接换字体申请注册&#xff0c;但是也没有下来。 这些商标名称主要是存在禁止注册或缺显&#xff0c;比如“柳林”以前…

蓝桥杯每日一题:修建灌木

题目来源: 第十三届蓝桥杯大赛软件赛省赛B组 爱丽丝要完成修建灌木的工作 有 N N N 棵灌木整齐的从左向右排成一排, 爱丽丝在每天傍晚会修建一棵灌木, 让灌木的高度变为 0 厘米. 修建灌木的顺序是从最左侧的灌木开始, 每天向右修建一棵灌木. 当修建了最右侧的灌木之后, 她会调…