2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > sql 子查询 嵌套查询_SQL子查询– SQL中的嵌套查询

sql 子查询 嵌套查询_SQL子查询– SQL中的嵌套查询

时间:2023-06-13 23:21:35

相关推荐

sql 子查询 嵌套查询_SQL子查询– SQL中的嵌套查询

sql 子查询 嵌套查询

In the real world, there are times when we need a particular set of data but we don’t have the exact details for getting the data set. In such cases, we try to get the information from the available set of information that we have. To achieve such target we use SQL subquery.

在现实世界中,有时候我们需要特定的数据集,但是我们没有获取数据集的确切细节。 在这种情况下,我们尝试从现有的可用信息集中获取信息。 为了实现这样的目标,我们使用SQL子查询。

SQL子查询 (SQL Subquery)

SQL subquery is a query nested inside another query. Most of the subqueries are used with WHERE clause of a query.

SQL子查询是嵌套在另一个查询中的查询。 大多数子查询与查询的WHERE子句一起使用。

SQL子查询规则 (SQL Subquery Rules)

A Subquery can be used with different SQL clauses like WHERE clause, HAVING clause and FROM clause.子查询可以与不同SQL子句(如WHERE子句,HAVING子句和FROM子句)一起使用。 Subqueries can be used with SELECT, UPDATE, INSERT and DELETE statements also.子查询也可以与SELECT , UPDATE , INSERT和DELETE语句一起使用。 The order of execution starts from subquery first then the main query.执行顺序从子查询开始,然后是主查询。 The Subquery must be enclosed in parentheses.子查询必须用括号括起来。 An ORDER BY command cannot be used in a subquery, even though the main query can use an ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER BY in a subquery.一个借命令命令不能在子查询中使用,即使主查询就可以使用ORDER BY。 GROUP BY命令可用于执行与子查询中的ORDER BY相同的功能。 When a subquery is used along with a comparison operator it should be on the right side of the comparison operator.当子查询与比较运算符一起使用时,它应位于比较运算符的右侧。

SQL子查询语法 (SQL Subquery Syntax)

SELECT column_name FROM table_name WHERE column_name OPERATOR (SELECT column_name FROM table_name WHERE condition);

The query inside the parenthesis after the OPERATOR is the subquery and the query outside the parenthesis is the main query.

OPERATOR之后在圆括号内的查询是子查询,而在圆括号外的查询是主查询。

SQL子查询示例 (SQL Subquery Example)

Let us consider the following two tables in order to understand Subquery in a better way.

让我们考虑以下两个表,以便更好地理解子查询。

Student

学生

Class

Please find below the MySQL queries to create the tables and insert the data.

请在下面MySQL查询中查找以创建表并插入数据。

CREATE TABLE `student` (`RollNo` INT NOT NULL,`Name` VARCHAR(45) NULL,`Age` INT NULL,`Gender` VARCHAR(45) NULL,PRIMARY KEY (`RollNo`));CREATE TABLE `class` (`ClassNo` INT NOT NULL,`Section` VARCHAR(45) NULL,`RollNo` INT NULL,PRIMARY KEY (`ClassNo`));INSERT INTO `student` (`RollNo`, `Name`, `Age`, `Gender`)VALUES(1, 'Amit', 12, 'M'),(2, 'John', 13, 'M'),(3, 'Diana', 14, 'F'),(4,'Henry', 15,'M');INSERT INTO `class` (`ClassNo`, `Section`, `RollNo`)VALUES(6, 'A', 1),(7, 'A', 2),(8, 'A', 3),(9,'B', 4);

Let us try to look into some examples using SQL subqueries.

让我们尝试研究一些使用SQL子查询的示例。

SQL subquery for getting all the student’s name with section ‘A’SQL子查询,用于获取“ A”部分的所有学生姓名

SELECT Name FROM student WHERE RollNo IN (SELECT RollNo FROM class WHERE section = 'A')

SQL SubQuery with IN operator

带IN运算符SQL SubQuery

SQL subquery with class greater than 7类大于7SQL子查询

SELECT * FROM student WHERE RollNo IN (SELECT RollNo FROM class WHERE ClassNo>7)

SQL subquery with Greater than operator

具有大于运算符SQL子查询

翻译自: /23945/sql-subquery-nested-query-sql

sql 子查询 嵌套查询

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。