0

SQL/MySql

I am having one table, in that table 25 columns are there. I need to select 24 columns from the table.

and i don't want to write all column names in select query,for example:

SELECT column1, column2...column24 FROM table1;

Is there any way to get (n-1),(n-2) columns from a select query?

n = number of columns.

user3410226
  • 17
  • 1
  • 8
  • check this http://stackoverflow.com/questions/21554661/select-last-n-of-columns-of-a-table-from-mysql it might help you – Abhik Chakraborty May 09 '14 at 07:09
  • There is no way. You have to write all column names (and I'm sure writing those 24 columns would have been faster than writing this question). Doesn't your SQL client support auto-completion where you just select those columns? –  May 09 '14 at 07:09
  • 1
    Put in short - nope. You'll have to write all desired columns. – Bud Damyanov May 09 '14 at 07:10
  • what is the purpose? if you need to use first n columns - just use them and do not take care of the rest of them – Yaugen Vlasau May 09 '14 at 07:13
  • @a_horse_with_no_name curiosity is more important than doing the things blindly. – user3410226 May 09 '14 at 07:13

1 Answers1

0

You can use the information_schema as in:

select GROUP_CONCAT(column_name) from information_schema.columns where table_schema = ? and table_name = ? and ordinal_position < ? order by ordinal_position

Stuff it in a procedure and you are good to go

Lennart - Slava Ukraini
  • 6,936
  • 1
  • 20
  • 32
  • One way I got is : 1.) CREATE TEMPORARY TABLE table2 AS (SELECT * FROM table1); 2.) ALTER TABLE table2 DROP COLUMN password; 3.) SELECT * FROM table2; 4.) DROP TABLE table2; I think this is the fine way. Fine,Thanks – user3410226 May 09 '14 at 07:26
  • Wow, that's the stupidest way of doing it (not referring to the answer). I just hope that people looking for the same thing won't stumble upon this comment, I've no words to stress enough how awful that solution is. – N.B. May 09 '14 at 07:42