如何显示SCHEMA中所有表的列标题

时间:2019-12-10 09:06:24

标签: mysql sql database

我想列出要在MySQL的行中显示的SCHEMA中的所有表和列标题,我似乎无法在线找到答案,将不胜感激。

例如,我的SCHEMA, table1, table2 and table3 ....中有50张桌子

表中的列数也不同。

我应该使用哪个查询来显示以下内容


| table1 | 01col1 header | 01col2 header | 01col3 header | 01col4 header |

| table2 | 02col1 header | 02col2 header | 02col3 header | 02col4 header | 02col5 header |

| table3 | 03col1 header | 03col2 header | 03col3 header |

| ...... |

| table50| 50col1 header | 50col2 header |

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将information_schema.columns与条件聚合一起使用。

select table_name,
        max(case when ordinal_position = 1 then column_name end) as col1name,
        max(case when ordinal_position = 2 then column_name end) as col2name,
        max(case when ordinal_position = 3 then column_name end) as col3name
from information_schema.`COLUMNS`
group by table_name;

如果所有表的最大列数烦人或有待更改,那么您可以修改此代码以构建准备好的语句。