简体   繁体   English

为什么这个查询不起作用,而这个却可以呢?

[英]Why doesn't this query work and this one does?

In MySQL Shell, two nearly identical queries. 在MySQL Shell中,两个几乎相同的查询。 One works, one doesn't. 一种有效,一种无效。 The difference is a key word. 区别是一个关键词。 Why is that? 这是为什么?

The working query just gets a salary of your choice depending on what limit number you use in the subquery. 工作查询只是根据您在子查询中使用的限制数而获得您选择的薪水。 So "Limit 4" would give you the fourth highest salary. 因此,“限额4”将为您提供第四高的薪水。

SELECT SALARY
FROM (
      SELECT DISTINCT SALARY
      FROM TBLEMPLOYEE
      ORDER BY SALARY DESC LIMIT 3
      ) RESULT
ORDER BY SALARY LIMIT 1

This successfully gives the third highest salary. 这成功地给出了第三高的薪水。

If I remove the word "result" it no longer works. 如果我删除“结果”一词,它将不再起作用。 What function does result serve? 结果起什么作用? Is that an alias? 那是别名吗? I get this: ERROR 1248 (42000): Every derived table must have its own alias 我得到这个:错误1248(42000):每个派生表必须具有自己的别名

Every derived table (AKA sub-query) must indeed have an alias. 每个派生表(AKA子查询)确实必须有一个别名。 Ie each query in brackets must be given an alias (AS whatever), which can the be used to refer to it in the rest of the outer query.Yes result is an alias.it could be any name in the place of result like myalias or something else. 也就是说,括号中的每个查询都必须被赋予一个别名(AS等等),可以在外部查询的其余部分中使用它来引用它。是的结果是一个别名。它可以是任何名称,例如myalias或者是其他东西。

SELECT SALARY
FROM (
      SELECT DISTINCT SALARY
      FROM TBLEMPLOYEE
      ORDER BY SALARY DESC LIMIT 3
      ) AS RESULT
ORDER BY SALARY LIMIT 1

more example 更多例子

SELECT ID
FROM (SELECT ID,
             msisdn 
      FROM (SELECT * FROM TT2) as myalias
     ) as anotheralias;

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

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