Search code examples
mysqlsqlordinal

SELECT in mysql using column number instead of name


Is there any way to do something like :

SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;

?


Solution

  • If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.

    I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.

    We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.