2

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.

5
  • Do you have any primary key in your table? Commented Feb 18, 2015 at 11:51
  • @Artista What's that got to do with selecting columns? Commented Feb 18, 2015 at 11:53
  • @Artista yes I have. Commented Feb 18, 2015 at 12:12
  • you can do SELECT * FROM some-table LIMIT 4,7 - this will fetch rows from index 4-10 Commented Feb 18, 2015 at 12:27
  • 1
    My question was about the columns not rows. Commented Feb 18, 2015 at 12:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

what if I know that ordering of the columns won't change.
Well, how often do you drop a table and recreate it with a changed ordering? However - I thought it is possible in postgresql, but made a check and it isn't. What is possible, is only 'SELECT * FROM something ORDER BY 2, 1'; for example, so in the ordering clause, for example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.