Home > Software design >  MySQL How do I use SELECT to get certain columns first and then the rest
MySQL How do I use SELECT to get certain columns first and then the rest

Time:10-22

I want to make a query which will get all columns (like SELECT * FROM Table) from a table but leading with a specific column (like SELECT Table.C, Table.*) without the duplicated column

>SELECT * FROM Table

A | B | C | D | ...

1 ; 2 ; 3 ; 4 ; ...

>SELECT Table.C, Table.* FROM Table

C | A | B | C | D | ...

3 ; 1 ; 2 ; 3 ; 4 ; ...

>?


C | A | B | D | ...

3 ; 1 ; 2 ; 4 ; ...

context

I have multiple tables with a similar structure each having columns like: Date, Time, Count, Quality, and then other columns more specific to the database, and I am making a program that uses a Query to get all of this information

CodePudding user response:

Sorry, the * wildcard always maps to all the columns of the respective table, not some of the columns.

The conventional way to specify the order of columns is to write out all the column names explicitly.

The only alternatives I can suggest are:

  • Change the order of columns in your base table.

  • Make a VIEW that reorders the columns as you would like.

  • Make a common table expression that reorders the columns as you would like.

  • Don't rely on the order of columns. Write application code to present the columns in whatever order you want, regardless of the order they appear in the result set.

  • Related