以原始顺序显示列名称?

时间:2016-08-02 01:35:46

标签: mysql sql-order-by

我试图在MySQL中一次显示一个列名,但问题是它是否按字母顺序显示它们。我使用的语法是:

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;

customer_contact表中,有三列cust_idcust_cell_numcust_email。当我使用上面的语法时,它会显示cust_cell_num而不是cust_id

将语法更改为以下内容时:

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;

按以下顺序显示列名称:cust_cell_numbercust_emailcust_id

如何让它按照实际显示在数据库中的顺序显示它们:cust_idcust_emailcust_cell_num

1 个答案:

答案 0 :(得分:4)

试试这个:

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;

请参阅此处的官方手册The INFORMATION_SCHEMA COLUMNS Table