Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 935175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:07:00+00:00 2026-05-15T21:07:00+00:00

I want to select all the rows in my database but I want them

  • 0

I want to select all the rows in my database but I want them in inverted sequence. Meaning, I want to use the first column data as the new entities and present entities as the first column. I think you got what I mean

Here is an illustration

id    |     name       | marks
-------------------------------
1     |    Ram         | 45
--------------------------------
2     |    Shyam       |  87

to

id    |   1     |    2     |
----------------------------
Name  |  Ram    |   Shyam  |
----------------------------
Marks |  45     |    87    | 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:07:00+00:00Added an answer on May 15, 2026 at 9:07 pm

    With a fixed and known columns, here’s how to do it (I took the liberty of naming the table “grades”):

    General Idea:

    To create a union of different queries and execute it.

    Since you need actual data as column headers, the first part of the union will look like:

    SELECT 'id', '1', '2', ....
    

    That query alone will duplicate the result, therefore we need to tell MySQL we need to have 0 rows by adding LIMIT 0, 0.

    Our first row of the union will contain 'Name', as well as all the data from “Name” column of the table. To get that line we need a query like:

    SELECT 'Name',
        (SELECT Name FROM grades LIMIT 0, 1),
        (SELECT Name FROM grades LIMIT 1, 1),
        (SELECT Name FROM grades LIMIT 2, 1),
        ...
    

    Using the same logic, our second row will look like:

    SELECT 'Marks',
        (SELECT Marks FROM grades LIMIT 0, 1),
        (SELECT Marks FROM grades LIMIT 1, 1),
        (SELECT Marks FROM grades LIMIT 2, 1),
        ...
    

    Getting the header:

    We need to produce a row from MySQL like:

    SELECT 'id', '1', '2', ... LIMIT 0, 0;
    

    To get that line we will use CONCAT() and GROUP_CONCAT() functions:

    SELECT 'id', 
        (SELECT GROUP_CONCAT(CONCAT(' \'', id, '\'')) FROM grades)
    LIMIT 0, 0;
    

    and we’re going to store that line into a new variable:

    SET @header = CONCAT('SELECT \'id\', ',
        (SELECT GROUP_CONCAT(CONCAT(' \'', id, '\'')) FROM grades),
        ' LIMIT 0, 0');
    

    Creating the lines:

    We need to create two queries like the following:

    SELECT 'Name',
        (SELECT Name FROM grades LIMIT 0, 1),
        (SELECT Name FROM grades LIMIT 1, 1),
        (SELECT Name FROM grades LIMIT 2, 1),
        ...
    

    Since we do not know in advance how many rows there are in our original table, we will be using variables to generate the different LIMIT x, 1 statements. They can be produced using the following:

    SET @a = -1;
    SELECT @a:=@a+1 FROM grades;
    

    Using this snippet, we can create our subqueries:

    SELECT GROUP_CONCAT(
        CONCAT(' (SELECT name FROM grades LIMIT ',
            @a:=@a+1,
            ', 1)')
        )
    FROM grades
    

    Which we will put into a variable names @line1, along with the first column data (which is the second column’s name):

    SET @a = -1;
    SET @line1 = CONCAT(
        'SELECT \'Name\',',
        (
            SELECT GROUP_CONCAT(
                CONCAT(' (SELECT Name FROM grades LIMIT ',
                    @a:=@a+1,
                    ', 1)')
                )
            FROM grades
        ));
    

    By following the same logic, the second line will be:

    SET @a := -1;
    SET @line2 = CONCAT(
        'SELECT \'Marks\',',
        (
            SELECT GROUP_CONCAT(
                CONCAT(' (SELECT Marks FROM grades LIMIT ',
                    @a:=@a+1,
                    ', 1)')
                )
            FROM grades
        ));
    

    Combining them all:

    Our three variables now contain:

    @header:
    SELECT 'id',  '1', '2' LIMIT 0, 0
    
    @line1:
    SELECT 'Name', (SELECT Name FROM grades LIMIT 0, 1),
        (SELECT name FROM grades LIMIT 1, 1)
    
    @line2:
    SELECT 'Marks', (SELECT Marks FROM grades LIMIT 0, 1),
        (SELECT marks FROM grades LIMIT 1, 1)
    

    We just need to create a final variable using CONCAT(), prepare it as a new query and execute it:

    SET @query = CONCAT('(',
        @header,
        ') UNION (',
        @line1,
        ') UNION (',
        @line2,
        ')'
    );
    
    PREPARE my_query FROM @query;
    EXECUTE my_query;
    

    Entire solution:

    (for testing and reference):

    SET @header = CONCAT('SELECT \'id\', ',
        (SELECT GROUP_CONCAT(CONCAT(' \'', id, '\'')) FROM grades),
        ' LIMIT 0, 0');
    
    SET @a = -1;
    SET @line1 = CONCAT(
        'SELECT \'Name\',',
        (
            SELECT GROUP_CONCAT(
                CONCAT(' (SELECT Name FROM grades LIMIT ',
                    @a:=@a+1,
                    ', 1)')
                )
            FROM grades
        ));
    
    SET @a := -1;
    SET @line2 = CONCAT(
        'SELECT \'Marks\',',
        (
            SELECT GROUP_CONCAT(
                CONCAT(' (SELECT Marks FROM grades LIMIT ',
                    @a:=@a+1,
                    ', 1)')
                )
            FROM grades
        ));
    
    SET @query = CONCAT('(',
        @header,
        ') UNION (',
        @line1,
        ') UNION (',
        @line2,
        ')'
    );
    
    PREPARE my_query FROM @query;
    EXECUTE my_query;
    

    Output:

    +-------+------+-------+
    | id    | 1    | 2     |
    +-------+------+-------+
    | Name  | Ram  | Shyam |
    | Marks | 45   | 87    |
    +-------+------+-------+
    2 rows in set (0.00 sec)
    

    Closing thoughts:

    • I’m still not sure why you need to transform rows into columns, and I’m sure the solution I presented is not the best one (in terms of performance).

    • You can even use my solution as a start and adapt it to a general purpose solution where the table column names (and the number of lines) are not known, using information_schema.COLUMNS as a source, but I guess that’s just going too far.

    • I strongly believe it is much better to put the original table into an array and then rotate that array, thus getting the data in the desired format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to select multiple rows from checkbox and want to update them all
I want to write a query to select from a table all rows with
I want to select all elements that have an accesskey defined on them. I
I want to select all internal links, which are in my document, but not
Suppose I have a column called fruits I want to select all of the
Put simply, I want to select all data in two columns from each of
I'm using System.Data.SQLite to query a database with c#/linq . The rows I want
Here's my situation: I want to SELECT all entries from a database WHERE id
I want to select all categories from a webservice. The webservice does not have
I want to select all the children of the body element before element with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.