简体   繁体   中英

select a column by their indexes rather than column_name in mysql

I just want to know is it even possible to provide the indexes of the columns or a range or indexes of the columns to select from a table.

like:

select 4-10 from some-table;

Many of you would ask why I want to do this, the reason is simple I have a table of around 12 to 15 columns and I want to perform some operations on only half of the columns its just not seems efficient to write down 7 to 10 fields to select from the table or even deselecting the remaining 5 to 8 columns (when using select * )

I didn't find anyone using this before anywhere so I am just curious is it even possible and if yes how?

Also for those who are concerned about ordering of fields can be altered or some fields can be even deleted for those I know what are the possibilities but what if I am sure the ordering of the table not gonna change as I myself maintaining it.

If you need to select only, why not create a view? This way, your scripts can be very small:

SELECT * FROM some-table-view

As for your question about indices: I am not sure, if there is some sql syntax that works like that. If there is, it would be really easy to cause errors, since any change in the table columns could affect the sql queries in an unwanted way.

No, this is not possible.

Since you can change the index of the columns, such queries would become unnmanagable and would select the wrong columns after such a change.

Write up the columns you need in your select. Many SQL tools provide a Generate Select function when clicking on the table that returns a select with all columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM