简体   繁体   English

如何知道mysql中我们可以使用子查询的所有地方?

[英]How to know where are all places we can use subqueries in mysql?

I am trying using subqueries in mysql, and came across the below statements where we can use subqueries in different places.我正在尝试在 mysql 中使用子查询,并遇到了以下语句,我们可以在不同的地方使用子查询。

select Salary from Employee where (select max(Salary) from Employee) < 400;
select Salary from Employee where Salary < (select max(Salary) from Employee);
select Salary from (select * from Employee) as seq1 where Salary < 400;

Now I am getting confused where are all places we can use subquery as there are multiple possibilities in using it.现在我很困惑我们可以在哪些地方使用子查询,因为使用它有多种可能性。

When I check the documentation for WHERE clause, it just mentioned giving condition as input, but nothing mentioned about where are all we can use subqueries to make the syntax correct当我查看 WHERE 子句的文档时,它只是提到将条件作为输入,但没有提到我们可以使用子查询使语法正确的地方

You can use a subquery anywhere that a table is allowed and you're just reading the table, such as the FROM or JOIN clauses.您可以在允许使用表的任何地方使用子查询,而您只需读取该表,例如FROMJOIN子句。 This is called a derived table .这称为派生表 That's your third example.那是你的第三个例子。 Since it has to be only for reading, you can't use it as the target for UPDATE or DELETE (but you can join with a subquery in an UPDATE or DELETE query).由于它只能用于读取,因此您不能将它用作UPDATEDELETE的目标(但您可以在UPDATEDELETE查询中加入子查询)。

You can use a subquery anywhere that an expression is allowed.您可以在任何允许表达式的地方使用子查询。 The subquery has to select a single value and must return at most one row (if it returns now rows it evaluates to NULL ).子查询必须 select 一个单一的值,并且必须最多返回一行(如果它现在返回行,它评估为NULL )。 A restriction is that you can't use a subquery in the WHERE clause of an UPDATE or DELETE query that's targeted to the same table;一个限制是您不能在针对同一个表的UPDATEDELETE查询的WHERE子句中使用子查询; in that cause you have to join with the subquery (see You can't specify target table for update in FROM clause ).因此,您必须加入子查询(请参阅You can't specify target table for update in FROM clause )。

This means that a subquery can be used in the SELECT list, in the WHERE clause, and in the HAVING clause.这意味着可以在SELECT列表、 WHERE子句和HAVING子句中使用子查询。 Even in the ON clause, although I've rarely seen examples of this.即使在ON子句中,尽管我很少看到这样的例子。

You can use a subquery as in place of the list of values in an IN expression, eg您可以使用子查询代替IN表达式中的值列表,例如

SELECT Salary
FROM Employee
WHERE id in (SELECT Emp_id FROM Employee_Department WHERE Dept_id = 100)

However, in my experience MySQL often optimizes these poorly, and they're better written as joins:然而,根据我的经验,MySQL 经常优化这些很差,他们最好写成连接:

SELECT e.Salary
FROM Employee AS e
JOIN Employee_Department AS d ON e.id = d.emp_id
WHERE d.dept_id = 100;

A SELECT query can also be used as the source of data in an INSERT query: SELECT查询也可以用作INSERT查询中的数据源:

INSERT INTO low_salary_employees
SELECT employee_id
FROM Employee
WHERE Salary < (SELECT MAX(Salary)/10 FROM Employee)

The first two example queries make use of scalar subqueries, ie subqueries that return a single row and column.前两个示例查询使用标量子查询,即返回单个行和列的子查询。 You can use such query everywhere you need a scalar value - so this opens a wide range of use cases, like in a select clause, in a where predicate, and so on.您可以在任何需要标量值的地方使用此类查询 - 因此这会打开广泛的用例,例如select子句、 where谓词等。

The third example is different: here, the subquery returns a set of records (potentially more than one row and more than one column).第三个例子不同:这里,子查询返回一组记录(可能不止一行和不止一列)。 This is called a derived table.这称为派生表。 You can use pretty much the same way you would use a regular table, so mostly in the from clause of a query.您可以使用与使用常规表几乎相同的方式,所以主要是在查询的from子句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM