I need to ask how can use Alias in Select Query,
I need this
SELECT (Complex SubQuery) AS A, (Another Sub Query WHERE ID = A) FROM TABLE
Table aliases can be used in SELECT lists and in the FROM clause to show the complete record or selective columns from a table. Table aliases can be used in WHERE, GROUP BY, HAVING, and ORDER BY clauses.
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.
Aliases are useful when table or column names are big or not very readable. These are preferred when there are more than one table involved in a query.
You cannot do this:
SELECT (Complex SubQuery) AS A, (Another Sub Query WHERE ID = A) FROM TABLE
You can however do this:
SELECT (Another Sub Query WHERE ID = A.somecolumn)
FROM table
JOIN SELECT (Complex SubQuery) AS A on (A.X = TABLE.Y)
Or
SELECT (Another Sub Query)
FROM table
WHERE table.afield IN (SELECT Complex SubQuery.otherfield)
The problem is that you cannot refer to aliases like this in the SELECT and WHERE clauses, because they will not have evaluated by the time the select or where part is executed.
You can also use a having
clause, but having clauses do not use indexes and should be avoided if possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With