Click here to Skip to main content
15,881,812 members
Articles / Database Development / MySQL
Tip/Trick

Sort a Column Inside an Already Created Table

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Sep 2012CPOL1 min read 14.6K   3   4
This tip will explain how to sort a column inside a table.

Introduction

This tip will explain how to sort a column inside a table.

In order to move the column, you will have to re-declare it (name and field type) and then specify the right position on the table.

Keep in mind that it is a bad practice to depend on the position columns are in a database.

This tip is here for two reasons:

  1. To help other users do this if they need it.
  2. To help me find this information again.

Using the Code

In order to make use of the code, you will have to use the SQL editing / execution environment of your database.

Let's imagine you have a table named "contents". Let's say that inside that table, you have some columns named "Spanish", "English", "German", and you would like to get them sorted alphabetically.

From:

CONTENTS
Spanish (text)
English (text)
German (text)

To:

CONTENTS
English (text)
German (text)

Spanish (text)

The SQL query you should execute to get the result shown in the previous sample is the following one:

SQL
alter table CONTENTS change Spanish Spanish text after German

You can see in the SQL query that what you are really making is altering the table by redefining the "Spanish" column with the same name (Spanish), and type (t<code>ext), placing the new "Spanish" column just after the "German" column.

This will make it...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer robotecnik
Spain Spain
I'm from Catalonia (in Spain) and a specialist in CNC, PLC, robotics and automation in general.

From 1998 I've been developing software applications for the automation market.

I'm using different technologies in each case to get the proper result, fieldbus communications, special electronics, special laser sensors, artificial vision, robot arms, CNC applications, PLC's...

www.robotecnik.com[^] - robots, CNC and PLC programming

Comments and Discussions

 
GeneralUnclear Pin
PIEBALDconsult7-Sep-12 4:32
mvePIEBALDconsult7-Sep-12 4:32 
QuestionRe: Unclear Pin
Joan M7-Sep-12 6:47
professionalJoan M7-Sep-12 6:47 
AnswerRe: Unclear Pin
John Brett9-Sep-12 21:02
John Brett9-Sep-12 21:02 
GeneralRe: Unclear Pin
Joan M9-Sep-12 21:16
professionalJoan M9-Sep-12 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.