教程 > sql 教程 > 阅读:39

sql 子查询——迹忆客-ag捕鱼王app官网

子查询,内部查询或嵌套查询是在一个 sql 查询中嵌入在 where 子句中另一个查询。

子查询用于返回将在主查询中用作条件的数据,以进一步限制要检索的数据。

子查询可以与 select、insert、update 和 delete 语句以及 =、<、>、>=、<=、in、between 等运算符一起使用。

子查询必须遵循一些规则

  • 子查询必须括在括号内。
  • 一个子查询在 select 子句中只能有一个列,除非子查询的主查询中有多个列来比较其选定的列。
  • 尽管主查询可以使用 order by,但不能在子查询中使用 order by 命令。group by 命令可用于执行与子查询中的 order by 相同的功能。
  • 返回多行的子查询只能与多个值运算符(例如 in 运算符)一起使用。
  • select 列表不能包含对检测为 blob、array、clob 或 nclob 的值的引用。
  • 子查询不能包含在 set 函数中。
  • between 运算符不能与子查询一起使用。但是,可以在子查询中使用 between 运算符。

带有 select 语句的子查询

子查询最常与 select 语句一起使用。基本语法如下 -

select column_name [, column_name ]
from   table1 [, table2 ]
where  column_name operator
   (select column_name [, column_name ]
   from table1 [, table2 ]
   [where])

示例

customers 表的数据如下

 ---- ---------- ----- ----------- ---------- 
| id | name     | age | address   | salary   |
 ---- ---------- ----- ----------- ---------- 
|  1 | ramesh   |  35 | ahmedabad |  2000.00 |
|  2 | khilan   |  25 | delhi     |  1500.00 |
|  3 | kaushik  |  23 | kota      |  2000.00 |
|  4 | chaitali |  25 | mumbai    |  6500.00 |
|  5 | hardik   |  27 | bhopal    |  8500.00 |
|  6 | komal    |  22 | mp        |  4500.00 |
|  7 | muffy    |  24 | indore    | 10000.00 |
 ---- ---------- ----- ----------- ---------- 

现在,让我们使用 select 语句检查以下子查询。

sql> select * 
   from customers 
   where id in (select id 
         from customers 
         where salary > 4500) ;

结果如下。

 ---- ---------- ----- --------- ---------- 
| id | name     | age | address | salary   |
 ---- ---------- ----- --------- ---------- 
|  4 | chaitali |  25 | mumbai  |  6500.00 |
|  5 | hardik   |  27 | bhopal  |  8500.00 |
|  7 | muffy    |  24 | indore  | 10000.00 |
 ---- ---------- ----- --------- ---------- 

带有 insert 语句的子查询

子查询也可以与 insert 语句一起使用。insert 语句使用从子查询返回的数据插入到另一个表中。子查询中选定的数据可以使用任何字符、日期或数字函数进行修改。

基本语法如下。

insert into table_name [ (column1 [, column2 ]) ]
   select [ *|column1 [, column2 ]
   from table1 [, table2 ]
   [ where value operator ]

示例

考虑一个与 customers 表结构相似的表 customers_bkp。现在要将完整的 customers 表复制到 customers_bkp 表中,可以使用以下 sql 语句。

sql> insert into customers_bkp
   select * from customers 
   where id in (select id 
   from customers) ;

带有 update 语句的子查询

子查询可以与 update 语句结合使用。使用带有 update 语句的子查询时,可以更新表中的单列或多列。

基本语法如下。

update table
set column_name = new_value
[ where operator [ value ]
   (select column_name
   from table_name)
   [ where) ]

示例

假设我们有 customers_bkp 表可用,它是 customers 表的备份。以下示例将 customers 表中所有 age 大于或等于 27 的客户的 salary 更新 0.25 倍。

sql> update customers
   set salary = salary * 0.25
   where age in (select age from customers_bkp
      where age >= 27 );

这将影响两行,最终 customers 表将具有以下记录。

 ---- ---------- ----- ----------- ---------- 
| id | name     | age | address   | salary   |
 ---- ---------- ----- ----------- ---------- 
|  1 | ramesh   |  35 | ahmedabad |   125.00 |
|  2 | khilan   |  25 | delhi     |  1500.00 |
|  3 | kaushik  |  23 | kota      |  2000.00 |
|  4 | chaitali |  25 | mumbai    |  6500.00 |
|  5 | hardik   |  27 | bhopal    |  2125.00 |
|  6 | komal    |  22 | mp        |  4500.00 |
|  7 | muffy    |  24 | indore    | 10000.00 |
 ---- ---------- ----- ----------- ---------- 

带有 delete 语句的子查询

子查询可以与 delete 语句结合使用,用法和上面提到的其他语句一样。

基本语法如下。

delete from table_name
[ where operator [ value ]
   (select column_name
   from table_name)
   [ where) ]

示例

假设我们有一个 customers_bkp 表可用,它是 customers 表的备份。以下示例从 customers 表中删除所有 age 大于或等于 27 的客户的记录。

sql> delete from customers
   where age in (select age from customers_bkp
      where age >= 27 );

这将影响两行,最终 customers 表将具有以下记录。

 ---- ---------- ----- --------- ---------- 
| id | name     | age | address | salary   |
 ---- ---------- ----- --------- ---------- 
|  2 | khilan   |  25 | delhi   |  1500.00 |
|  3 | kaushik  |  23 | kota    |  2000.00 |
|  4 | chaitali |  25 | mumbai  |  6500.00 |
|  6 | komal    |  22 | mp      |  4500.00 |
|  7 | muffy    |  24 | indore  | 10000.00 |
 ---- ---------- ----- --------- ---------- 

查看笔记

扫码一下
查看教程更方便
网站地图