首页 > 其他分享 >9. 子查询/INSERT/UPDATE/DELETE/REPLACE(未完成)

9. 子查询/INSERT/UPDATE/DELETE/REPLACE(未完成)

时间:2023-05-31 21:46:58浏览次数:35  
标签:INSERT +------+ t2 查询 UPDATE t1 mysql REPLACE select

一. 子查询

子查询就是指在一个select语句中嵌套另外一个select语句。同时子查询必须包含括号。MySQL 5.6之前,子查询的性能较差,但是从5.6开始,不存在性能差的问题。

select a from t1 where a > any(select a from t2);

1. select a from t1是外部查询(outer query)

2. (select a from t2) 是子查询

一般来说子查询嵌套与外部查询中,可以将两个或两个以上子查询嵌套

 

1. 子查询的使用

1.1 ANY / SOME

如果外部查询列的结果和子查询列的结果比较得到为True,则返回比较值为True的(外查询)的记录

[[email protected]][dbt3_s1]> create table t1(a int);
Query OK, 0 rows affected (0.34 sec)

[[email protected]][dbt3_s1]> create table t2(a int);
Query OK, 0 rows affected (0.02 sec)

[[email protected]][dbt3_s1]> insert into t1 values(10),(4);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

[[email protected]][dbt3_s1]> insert into t2 values(12),(13),(5);
Query OK, 3 rows affected (0.30 sec)
Records: 3  Duplicates: 0  Warnings: 0

[[email protected]][dbt3_s1]> select a from t1;
+------+
| a    |
+------+
|   10 |
|    4 |
+------+
2 rows in set (0.00 sec)

[[email protected]][dbt3_s1]> select a from t2;
+------+
| a    |
+------+
|   12 |
|   13 |
|    5 |
+------+
3 rows in set (0.00 sec)

[[email protected]][dbt3_s1]> select a from t1 where a > ANY(select a from t2); -- 子查询返回(12,13,5)
+------+                                                                      -- t1 a列只要大于(12,13,5)中的任一值
| a    |                                                                      -- 即t1.a > t2.a为True,则返回t1.a
+------+
|   10 |
+------+
1 row in set (0.07 sec)
-- 这个查询可以解释为,t1表内a列的值 大于t2表中a列的任意(any)一个值(t1.a > any(t2.a) == true),则返回t1.a的记录

ANY 关键词必须与一个 比较操作符 一起使用:  = ,  > ,  < ,  >= ,  <= ,  <> (这个是!=的意思)

子查询中 SOME 和 ANY 是同一个意思

 

1.2. IN

in 是 ANY 的一种特殊情况: "in" equals "= any"

[[email protected]][dbt3_s1]> insert into t1 values(5);
Query OK, 1 row affected (0.26 sec)

[[email protected]][dbt3_s1]> select a from t1 where a = ANY(select a from t2);
+------+
| a    |
+------+
|    5 |
+------+
1 row in set (0.00 sec)

[[email protected]][dbt3_s1]> select a from t1 where a in (select a from t2);
+------+
| a    |
+------+
|    5 |
+------+
1 row in set (0.01 sec)

 select a from t1 where a in (select a from t2); 是用的比较多的一种语法

 

1.3. ALL

如果外部查询的列的结果和子查询的列的 所有结果 比较得到为True的话,则返回比较值为True的(外查询)的记录

[[email protected]][dbt3_s1]> truncate t1;
Query OK, 0 rows affected (0.20 sec)

[[email protected]][dbt3_s1]> truncate table t2;
Query OK, 0 rows affected (0.06 sec)

[[email protected]][dbt3_s1]> insert into t1 values(10),(4);
Query OK, 2 rows affected (0.29 sec)
Records: 2  Duplicates: 0  Warnings: 0

[[email protected]][dbt3_s1]> insert into t2 values(5),(4),(3);
Query OK, 3 rows affected (0.30 sec)
Records: 3  Duplicates: 0  Warnings: 0

[[email protected]][dbt3_s1]> select a from t1 where a > all(select a from t2);
+------+
| a    |
+------+
|   10 |  -- (10 > 5, 4, 3 为True) 而(4 >5, 4, 3 为False)
+------+
1 row in set (0.00 sec)

ALL 关键词必须与一个 比较操作符 一起使用
NOT IN  是  <> ALL 的别名

 

2. 子查询的分类

独立子查询

不依赖外部查询而运行的子查询

[[email protected]][dbt3_s1]> select a from t1 where a in (1,2,3,4,5);
+------+
| a    |
+------+
|    4 |
+------+
1 row in set (0.00 sec)

相关子查询

引用了外部查询的子查询

-- 这个例子中,子查询使用了外部的列t2.a
[[email protected]][dbt3_s1]> select a from t1 where a in (select * from t2 where t1.a = t2.a);
+------+
| a    |
+------+
|    4 |
+------+
1 row in set (0.00 sec)

 

3. 子查询的优化

MySQL 5.6之前

  在MySQL 5.6之前,优化器会把子查询重写成exists的形式

 

标签:INSERT,+------+,t2,查询,UPDATE,t1,mysql,REPLACE,select
From: https://www.cnblogs.com/gavin-zheng/p/17383281.html

相关文章

  • Efficient Correction of Single InsertionlDeletion and Multi-Substitution Errors
    EfficientCorrectionofSingleInsertionlDeletionandMulti-SubstitutionErrorsG.J.Han,Y.L.Guan,K.Cai,K.S.Chan,andL.J.KongA!JshYlc�Atwo-stagesynchronizationalgorithmisproposedtocorrectsingleinsertion/deletionandmulti-substitution......
  • mysql中update会锁表吗
    MySQL中的update操作会不会锁表是一个值得关注的问题,因为这会影响到并发性能和系统的响应速度。本文将从两个方面探讨这个问题:一是有没有索引的情况下,二是MySQL开启了自动提交事务和手动提交事务的情况下。首先,在没有索引的情况下,MySQL的update操作会锁整个表。这是因为在......
  • mysql索引损坏,Record in index `log_time` of table `lts`.`lts_job_log_po` was not
    【1】错误信息[ERROR][MY-012869][InnoDB]Recordinindex`log_time`oftable`lts`.`lts_job_log_po`wasnotfoundonupdate:TUPLE(info_bits=0,2n_cmp=2,fields)2023-05-29T23:03:05.146242Z193[ERROR][MY-013183][InnoDB]Assertionfailure:row0upd.cc:......
  • 使用 Collections中的replaceAll方法 替换list中的指定元素
    以下实例演示了如何使用Collections类的replaceAll()来替换List中所有的指定元素:importjava.util.Arrays;importjava.util.Collections;importjava.util.List;publicclassImoocStudent{publicstaticvoidmain(String[]args)throwsException{......
  • insert
    INSERTINTOtable_name(column1,column2,column3,...)VALUES(value1,value2,value3,...) 一定要按顺序吗?只插入最后一个列的值可以吗? ......
  • Python 数据库Insert语句脚本生成工具(SQL Server)
    编写这个小工具,是因为平时部署项目的时候,需要导出一些公共的数据(权限、参数设置等),覆盖插入正式环境。话不多说,直接上代码:importpyodbcimportwarningsimportdecimalimportwinregimportosimportconfigparserimporttimeimportdatetimewarnings.filterwarnings('igno......
  • 执行一条update会发生什么?
    1、和查询语句一样也会走一遍连接器、解析器、预处理器、优化器、执行器 2、执行器在更新数据前,会先查看bufferpool中是否存在要更新的数据,如果不存在则从磁盘加载到bufferpool,存在则进行修改 3、innodb把旧值写入undolog 4、innodb把新值写到bufferpool 5、innod......
  • Windows Server 2022 中文版、英文版下载 (updated May 2023)
    WindowsServer2022正式版,2023年5月更新请访问原文链接:https://sysin.org/blog/windows-server-2022/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org早期直观体验版本21H2,根据名称预计今年秋季发布正式版(已经发布)设置和控制面板仍然混乱,麦德龙风格和经典风......
  • Windows 11 22H2 中文版、英文版 (x64、ARM64) 下载 (updated May 2023)
    Windows11,version22H2,2023年5月更新请访问原文链接:https://sysin.org/blog/windows-11/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org全新推出Windows11全新Windows体验,让您与热爱的人和事物离得更近。获得全新视角Windows11提供一个让人平静而富......
  • js replace 和 replaceAll
    js中的replace方法和自定义replaceAll方法<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>String字符串测试</title></head><body><inputtype="submit"va......