Return order of MySQL SHOW COLUMNS

人走茶凉 提交于 2019-12-08 16:38:16

问题


I need to find the columns in a specific table, which is no problem:

SHOW COLUMNS FROM tablename LIKE '%ColumnPrefix%';

But I need to know what order they will be returned, preferably by choosing to order the results ascending alphabetically. I have had no luck with using ORDER BY.

Any ideas?


回答1:


You can query the table INFORMATION_SCHEMA.COLUMNS to get the information that SHOW COLUMNS gives you, plus it allows you to use ORDER BY or any other SQL syntax you might want to use:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tablename'
  AND column_name LIKE 'ColumnPrefix%'
ORDER BY column_name



回答2:


Since I have had the exact same problem I will complete Mark's answer. Here is the exact long version of the 'show columns from table' query:

SELECT 
    `column_name` AS `Field`, 
    `column_type` AS `Type`, 
    `is_nullable` AS `Null`, 
    `column_key` AS `Key`, 
    `column_default` AS `Default`, 
    `extra` AS `Extra` 
FROM 
    `information_schema`.`columns` 
WHERE 
    `table_name`='mytable';

With this query you can order the result however you wish by adding ORDER BY.



来源:https://stackoverflow.com/questions/2544751/return-order-of-mysql-show-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!