0

how to write a select query with one or two column excluded instead of list down required columns.

for example we have a table Table1 with 10 columns (col_1,col_2,col_2 ..... col_10) and we just want select col_1 to col_8.

instead of writing like

Select col_1, col_2, col_3 .... col_8 from Table1

should we select like this

Select -(col_9, col_10) * from Table1
Robert
  • 25,425
  • 8
  • 67
  • 81
Wasan
  • 1
  • 1
  • 2

2 Answers2

0

This is possible but you would have to use dynamic SQL or a stored procedure. It would still mean having to specify the start and end column using fixed column names and indexes and if the columns weren't in running order (colN, colN+1, etc.) then it would become messy.

So I suppose your answer it, just type it out. The benefit you will gain from doing something cleaver is small.

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
0

Any ideas?

I think we can select column name first then write into query.

1 Select column with excluded.

SHOW COLUMNS FROM _table WHERE FIELD NOT IN ('column1','column2')

2 Then, Use the column result we've got from above query to write to select query column.

SELECT {result_aboved} FROM _table WHERE 1

but I don't think it can be only once query for this case. you have to query twice times at least.

Zeigen
  • 126
  • 2
  • 7