Sangeetha Krishnan
Sangeetha Krishnan

Reputation: 1001

select values in column reverse order

I need to select the records wit columns in reverse order table structure. For Example, If My table struture is like this

class   name        rank  
------  ----------  ------
A       Nija             2
D       cruz             3
B       Antony           4
C       silverster       5
A       febin            1   

means I want the result as

  rank  name        class 
------  ----------  ------
     2  Nija        A     
     3  cruz        D     
     4  Antony      B     
     5  silverster  C     
     1  febin       A     

you can say that select the columns like rank,name,class. But My Table columns will increase in dynamically. I want the result if my table contains any number of columns but the result should be in reverse of the table structure. Here i ll select all columns only.

Upvotes: 0

Views: 738

Answers (2)

AnandPhadke
AnandPhadke

Reputation: 13496

try this:

SET @myval=(select group_concat(COLUMN_NAME)from (
SELECT ORDINAL_POSITION, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'Table1'
order by ORDINAL_POSITION desc) a);

SET @strsql=CONCAT('select ',@myval,' from Table1');

PREPARE stmt FROM @strsql; 
EXECUTE stmt;

SQL FIDDLE DEMO

Upvotes: 1

Muthu Kumaran
Muthu Kumaran

Reputation: 17910

Why don't you do simply

SELECT rank, name, class FROM table_name;

Upvotes: 0

Related Questions