我想列出 SCHEMA 中的所有表和列标题,以显示在 MySQL 中的行中,我似乎无法在线找到答案,非常感谢任何帮助。
例如,我的 SCHEMA 中有 50 个表,table1、table2 和 table3 ....
表中的列数也不同。
我应该使用什么查询来显示以下内容
| 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 |
谢谢!
最佳答案
您可以将 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;
如果所有表的最大列数太大或容易发生变化,那么您可以调整此代码来构建准备好的语句。
关于mysql - 如何显示 SCHEMA 中所有表的列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59263616/