4

is it possible in mysql to select a value from a table by specifying the column number instead of the column name ?

3

3 Answers 3

5

No, you can not use the ordinal value of a column in the SELECT clause.

Column order is irrelevant to a database; the ordinal value is based on the list of columns in the SELECT clause. The ordinal value is supported after the SELECT clause - IE: in the GROUP BY, and ORDER BY. That said, using ordinals is not a recommended approach because ordinals are brittle -- if someone changes the column order in the SELECT clause, the query can be negatively impacted.

1
  • but in regular sql you can, SELECT 1, 2, 3 FROM table
    – J3STER
    Mar 8, 2017 at 17:32
1

I don't think it is possible "directly" BUT with a query linked to the ORDINAL_POSITION field of the COLUMNS table from the information_schema should do the work!

Edit: COLUMNS table contains all the fields of all tables (and their positions)

1
  • 1
    You will need a PL type language or dynamic sql (not in MySQL) that can build sql strings from queries and execute them.
    – nate c
    Dec 20, 2010 at 17:56
0

I think for that you have to execute a single query using variables recursively. like using for loop to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.