It seems you are wondering why the second subquery is executed, but not multiplying. Although it should be the opposite.
The fact that multiplication cannot be done follows from the fact that aliases declared in a SELECT can be used in GROUP BY , ORDER BY and HAVING expressions. About the fact that the alias can be used in the select itself again is not said anywhere else, this is not in the standard, and it is not implemented in the mysql extensions. What actually can be seen in the description of the SELECT syntax in the documentation .
A select_expr can be given an alias using AS alias_name . The alias is used in the HAVING clauses.
As for the use of aliases in subqueries, the following bug was published for version 5.7.9. It concerns just the use of the external query aliases in the where conditions of the subquery. This topic contains the following developer comment:
1. Why is it stopped working?
A. It stopped working because we were expanding to the SQL standard. And when examining some of the crashes, it was decided. But the crash bug was used in the WHERE clause, not in the SELECT list.
2. Why standard SQL was changed? (or it didn’t previously been given to the standard?)
A: This is a construction of the SQL interface to the standard. The standard has never allowed references to aliases, except within the ORDER BY clause.
3. How does it work again?
It is not necessary to clarify the issue. Thus, we are reopening the bug.
Here is the background for the original decision:
Contrary to the law in which it is a clause (there is no reason for compliance with the regulations). the same phase of query execution. But the support in 5.6 was quite arbitrary:
Given this: create table t1(a int, b int)
Alias in SELECT list is not valid:
select a+b as c,c+1 from t1; ERROR 1054 (42S22): Unknown column '`c`' in 'field list'
Reference to c is valid:
select a+b as c,(select c+1) from t1;
And subquery must be after definition of alias:
select (select c+1),a+b as c from t1; ERROR 1247 (42S22): Reference `c` not supported (forward reference in item list)
It is easy to say that it’s a list of advertisements. However, we’ll try to make it clear. But referencing aliases in subqueries on the WHERE clause will not be reimplemented.
This shows that:
- the use of aliases in subqueries is an extension of Mysql (although it’s not really written about the documentation anywhere, there are, however, references to
HAVING in the subqueries). - the use of aliases was prohibited in the
where clause of subqueries due to some errors in their use. - the use of the alias must be after the alias has been defined
- Well, as an alternative to your original error , it is suggested
(select amt*10) or 10*(select amt) both of these options should work properly.
Regarding this bug and references to aliases in the HAVING subquery, the documentation:
The SQL standard requires that you use the aggregate functions. However, MySQL supports the subsections as well.
as well as from changelog version 5.7.11
There are no rules for SQL SQL 5.7.8. However, this is a frequently used extension.
In general, I do not know how this having corresponds to the general use of external aliases in subqueries, but it is worth checking whether all the functionality in recent versions has earned or not. I have nothing to check on.
10*(select amt)will be :) - teran