Whether the MySQL select field alias can be used in select or where

Source: Internet
Author: User

Select Column1+10 as c1,c1+10 as C2 from table1;
Want to achieve the above effect, the result in MySQL inside error, the hint can not find C1 this column;

-Different databases are not the same

It is not generally supported to continue using aliases in select or in the WHERE statement

If it is in use, you can try whether it supports:

Select Column1+10 as C1, (select C1) +10 as C2 from table1;

But in the groupby can be used

---some explanatory references

https://stackoverflow.com/questions/16715504/ mysql-define-a-variable-within-select-and-use-it-within-the-same-select/16715618#16715618

Https://stackoverflow.com/questions/6081436/how-to-use-alias-as-field-in-mysql

The SQL statement execution in MySQL is in a certain order, as follows:
1. From
2. On
3. Join
4. Where
5. GROUP BY
6. With
7. Having
8. Select
9. Distinct
Ten. ORDER BY
One. Limit
A SQL will pass these 11 steps, each step in the middle will generate a virtual table, the next step is to filter and query in the previous virtual table, the following is assumed, after 7 steps, that is, after having this step, the resulting virtual table is assumed:

id  column1 column2 alias(having后的别名)1      10       10      aaa2      20 20 bbb

Now to select this step, your query field is column1+10 as C1, then the SQL parser in this virtual table can find Column1, then the calculation and set alias success, now you want to c1+10, it found that this virtual table does not exist in this field, Then will be an error, if you want to do this: alias as XXX, then also will not error, because after having the filter, the individual name field is already in the virtual table, so the truth is very simple, select the order of execution is ranked in the 8th step, and select is for the above steps to build the virtual table operation, so you want to use the field, if the virtual table does not exist, then will be error, if the landlord of the sentence SQL to execute, it can only be changed to select Column1+10 as C1,column1+10+10 as C2 from table1;

Whether the MySQL select field alias can be used in select or where

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.