Osiris93 Osiris93 - 4 months ago 8
SQL Question

Display column names in their original order?

I'm trying to display column names one at a time in MySQL but the problem is it keeps displaying them in alphabetical order. The syntax I'm using is:

select column_name from information_schema.columns where table_schema =
'customer_info' and table_name='customer_contact' order by column_name LIMIT 1 OFFSET 0;


In the
customer_contact
table there are three columns which are
cust_id
,
cust_cell_num
and
cust_email
. When I use the syntax above it displays the
cust_cell_num
instead of
cust_id
.

When changing the syntax to the following:

select column_name from information_schema.columns where table_schema =
'customer_info' and table_name='customer_contact' order by column_name LIMIT 3 OFFSET 0;


It displays the column names in the following order:
cust_cell_number
,
cust_email
,
cust_id
.

How can I get it to display them in the order they actually appear on the database which is:
cust_id
,
cust_email
,
cust_cell_num
?

JPG JPG
Answer

Try this:

select column_name
from information_schema.columns 
where table_schema = 'customer_info' 
and table_name = 'customer_contact' 
order by ordinal_position
limit 3 offset 0;

See official manual here The INFORMATION_SCHEMA COLUMNS Table

Comments