2016-08-02 141 views
0

我試圖在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

回答

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

+0

謝謝你,成功了! :d – Osiris93