Most SQL experts will say that you cannot reuse an alias in a selection at the same level; often used for this CTE; or one wraps the request as a subquery so that an alias can be referenced. However, mySQL seems to allow this situation if the alias is referenced in a subquery inside select itself; so it’s not technically on the same level.
DEMO:
SELECT CONCAT(a, b) AS c1, CONCAT((SELECT c1), 2)
FROM (SELECT 'a' a, 'b' b, 'c' c UNION ALL
SELECT '1', '2', '3') t1;
SELECT 1 a, 2 b, (SELECT A+B) c
, concat((SELECT a),(SELECT b)) d
, greatest((SELECT a),(SELECT b), (SELECT c))
Both of the above queries work .. yes; they work. (or do a really good job to make it look like it works)
So far this is not so: as one would expect.
SELECT CONCAT(a, b) AS c1, CONCAT(c1, 2)
FROM (SELECT 'a' a, 'b' b, 'c' c UNION ALL
SELECT '1', '2', '3') t1;
So the question here is twofold:
: MySQL