简体   繁体   English

mysql“ where 3”中的where子句

[英]where clause in mysql “where 3”

I have fetch 3 highest salary from my table Here my query 我从表中拿到了最高的3薪水这是我的查询

SELECT DISTINCT salary
FROM salary e1
WHERE 3 = (
SELECT count( DISTINCT salary )
FROM salary e2
WHERE e1.salary >= e2.salary ) 

It's working fine but I want know, why we put condition "Where 3"? 工作正常,但我想知道,为什么我们将条件设置为“ Where 3”? how it is performing? 效果如何?

Your query is slow not because of that 3 but because it unnecessarily uses in inner query. 您的查询很慢,不是因为那3而是因为它在内部查询中不必要地使用。 The answer provided by @spencer7593 explains how it works and you can see it does the same thing again and again. @ spencer7593提供的答案说明了它是如何工作的,您可以看到它一次又一次地执行相同的操作。

The query can be written as simple as this: 查询可以这样写:

SELECT DISTINCT e1.salary
FROM salary e1
ORDER BY e1.salary DESC
LIMIT 3;

Note: 注意:

The alias e1 is not needed but I let it in the FORM clause and used it in the ORDER BY clause to make clear what represents each instance of salary (both the table and the field have this name). 不需要别名e1 ,但我在FORM子句中使用了别名,并在ORDER BY子句中使用了别名,以明确表示salary每个实例的含义(表和字段均具有此名称)。

How it works: 这个怎么运作:
It ORDER s the rows from table "salary" BY field salary descending (greatest salaries first), SELECT s only DISTINCT values (this is where you wrote it well) and LIMIT s the result set to 3 rows. ORDER期从表“工资”的行BY领域salary下降(最大薪水第一), SELECT唯一DISTINCT值(这是你写的很好)和LIMIT S中的结果设置为3行。

In fact, it stops the execution after it finds the 3 greatest distinct values from column salary . 实际上,它从column salary找到3个最大的不同值后就停止执行。

If the table has an index on field salary it runs like the wind. 如果表中有外地salary指数,它的运行就像风一样。 If it hasn't such an index, create it now: 如果没有这样的索引,请立即创建:

ALTER TABLE `salary` ADD INDEX (`salary`);

The equality comparison is comparing two values. 相等比较是比较两个值。 A constant value of 3 on the one side, and the value returned by a subquery on the other side. 一侧为3的常量值,另一侧为子查询返回的值。

If the result of the comparison is TRUE for a row being evaluated, then the row will be returned. 如果对于要评估的行,比较结果为TRUE,则将返回该行。 Otherwise, the row won't be returned. 否则,该行将不返回。

The "trick" here is the subquery on the right side. 这里的“技巧”是右侧的子查询。 That's what we call a "correlated" subquery. 这就是我们所谓的“相关”子查询。 That gets run for every row that's being evaluated. 它将针对正在评估的每一行运行。

Basically, the operation can be described like this... 基本上可以这样描述操作...

Access a row from salary table, and get the value of the salary column from it. salary表中访问一行,并从中获取salary列的值。

Run another query, using the value of salary column to get a count. 运行另一个查询,使用salary列的值获取计数。

Compare that count to a constant value of 3. 将该计数与恒定值3进行比较。

If the comparison yields TRUE, return the row, otherwise discard it. 如果比较结果为TRUE,则返回该行,否则将其丢弃。

Access the next row from the salary table and get the salary value. 访问salary表的下一行并获取薪水值。

Run another query, using the value we just got, to get a count. 使用我们刚得到的值运行另一个查询以获取计数。

Compare the count to a constant value of 3. 将计数与恒定值3进行比较。

If the comparison yields TRUE, return the row, otherwise discard. 如果比较结果为TRUE,则返回该行,否则丢弃。

And just repeat that until we've done it for all rows in the salary table. 重复一遍,直到我们对salary表中的所有行都完成了。

The final step is the DISTINCT operation, which eliminates any duplicates that we would otherwise be returning. 最后一步是DISTINCT操作,它消除了我们否则将返回的所有重复项。

That's how it's performing. 那就是它的表现。


In terms of performance, on large sets, a correlated subquery is going to each our lunch, and our lunch box too. 就性能而言,在大集合上,相关联的子查询将用于我们的午餐以及午餐盒。 This is not the only query pattern that can return this result, there are other queries that will do less work to return an equivalent result. 这不是唯一可以返回此结果的查询模式,还有其他查询在返回同等结果方面的工作较少。

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

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