
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Return order of MySQL SHOW COLUMNS?
To return order of MySQL SHOW COLUMNS, you need to use ORDER BY clause. The syntax is as follows −
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ‘yourTableName’ AND column_name LIKE 'yourStartColumnName%' ORDER BY column_name DESC;
Let us create a table in database TEST. The query to create a table is as follows −
mysql> create table OrderByColumnName -> ( -> StudentId int, -> StudentFirstName varchar(10), -> StudentLastName varchar(10), -> StudentAddress varchar(20), -> StudentAge int, -> StudentMarks int -> ); Query OK, 0 rows affected (1.81 sec)
Case 1 −
In this, the result is in descending order. Here is the query to return order of show column in MySQL −
mysql> SELECT COLUMN_NAME -> FROM INFORMATION_SCHEMA.COLUMNS -> WHERE table_name = 'OrderByColumnName' -> AND column_name LIKE 'student%' -> ORDER BY column_name DESC;
The following is the output −
+------------------+ | COLUMN_NAME | +------------------+ | StudentMarks | | StudentLastName | | StudentId | | StudentFirstName | | StudentAge | | StudentAddress | +------------------+ 6 rows in set (0.00 sec)
Case 2 − If you want the result in ascending order, there is no need to write ASC keyword because by default result will be in ascending order.
The query is as follows −
mysql> SELECT COLUMN_NAME -> FROM INFORMATION_SCHEMA.COLUMNS -> WHERE table_name = 'OrderByColumnName' -> AND column_name LIKE 'student%' -> ORDER BY column_name;
The following is the output −
+------------------+ | COLUMN_NAME | +------------------+ | StudentAddress | | StudentAge | | StudentFirstName | | StudentId | | StudentLastName | | StudentMarks | +------------------+ 6 rows in set (0.00 sec)
- Related Articles
- How can we get more details about columns of an existing table than return by MySQL SHOW COLUMNS statement?
- Exclude certain columns from SHOW COLUMNS in MySQL?
- Can we alter order of columns in MySQL?
- Order a MySQL table by two columns?
- Easy way to re-order columns in MySQL?
- How to order MySQL rows by multiple columns?
- Order by a function of two columns in a single MySQL query
- Order by multiple columns not working as expected in MySQL?
- How to order return duplicate column values only once in MySQL?
- Write a MySQL query equivalent to “SHOW TABLES” in sorted order?
- Return similar names from different columns with distinct space allocations in MySQL?
- How to order by the highest value from two columns in MySQL?
- How can I return the values of columns from MySQL table as a set of values?
- Do we have any other statement in MySQL instead of SHOW COLUMNS to get the list of columns in an existing table?
- Change the order of the Bootstrap grid columns

Advertisements