子查询或内部查询或嵌套查询是另一个SQL查询中的查询,并嵌入在WHERE子句中。
Sub Queries - 语法
子查询最常与SELECT语句一起使用。基本语法如下-
SELECT column_name [, column_name ] FROM table1 [, table2 ] WHERE column_name OPERATOR (SELECT column_name [, column_name ] FROM table1 [, table2 ] [WHERE])
Sub Queries - 示例
考虑具有以下记录的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 | Learnfk | 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 | Learnfk | 24 | Indore | 10000.00 | +----+----------+-----+---------+----------+
INSERT子查询
子查询也可以与INSERT语句一起使用, INSERT语句使用从子查询返回的数据插入另一个表。可以使用任何字符,数字功能修改子查询中的选定数据。
基本语法如下。
INSERT INTO table_name [ (column1 [, column2 ]) ] SELECT [ *|column1 [, column2 ] FROM table1 [, table2 ] [ WHERE VALUE OPERATOR ]
INSERT子查询 - 示例
考虑具有与CUSTOMERS表相似结构的表CUSTOMERS_BKP,现在要将完整的CUSTOMERS表复制到CUSTOMERS_BKP表中,可以使用以下语法。
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) ]
UPDATE子查询 - 示例
假设无涯教程有CUSTOMERS_BKP表可用,它是CUSTOMERS表的备份,下面的示例对AGE大于或等于27的所有客户在CUSTOMERS表中将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 | Learnfk | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
DELETE子查询
子查询可以与DELETE语句结合使用,就像上面提到的任何其他语句一样。
DELETE FROM TABLE_NAME [ WHERE OPERATOR [ VALUE ] (SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ]
DELETE子查询 - 示例
假设无涯教程有一个CUSTOMERS_BKP表,它是CUSTOMERS表的备份。以下示例从AGE大于或等于27的所有客户中,从CUSTOMERS表中删除记录。
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 | Learnfk | 24 | Indore | 10000.00 | +----+----------+-----+---------+----------+
参考链接
https://www.learnfk.com/sql/sql-sub-queries.html
标签:CUSTOMERS,NAME,SubQueries,AGE,无涯,查询,SQL,WHERE,SELECT From: https://blog.51cto.com/u_14033984/9276115