简体   繁体   English

如何显示 SCHEMA 中所有表的列的 header

[英]How can I display the header of columns of all tables within a SCHEMA

I would like to list out all the tables and column headers within a SCHEMA to be displayed in Rows within MySQL, I can't seem to find an answer online and would greatly appreciate any help.我想列出要在 MySQL 内的行中显示的 SCHEMA 中的所有表和列标题,我似乎无法在网上找到答案,非常感谢任何帮助。

eg I have 50 tables within my SCHEMA, table1, table2 and table3....例如,我的SCHEMA, table1, table2 and table3....

The number of columns within the tables are also different.表中的列数也不同。

What query should I use to display the following我应该使用什么查询来显示以下内容


| 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 |

Thank you!谢谢!

You could use information_schema.columns together with conditional aggregation.您可以将 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;

If the max number of columns over all the tables is annoyingly large or subject to change then you could adapt this code to build a prepared statement.如果所有表的最大列数非常大或可能会发生变化,那么您可以修改此代码以构建准备好的语句。

I happened to be noodling around with this very topic a month or two ago, so here's a really simple proof of concept that I wrote.一两个月前我碰巧在讨论这个话题,所以这是我写的一个非常简单的概念证明。 It basically pretty prints first your tables and then all the columns of your tables, in HTML.它基本上首先漂亮地打印您的表格,然后打印表格的所有列,在 HTML 中。

<HTML>
<HEAD>
<TITLE>List Schema</TITLE>
</HEAD>
<BODY>
<?php
# Note, untested sample code NO WARRANTY expressed or implied.
# If this causes your computer to spontaneously initiate a complete
# matter-to-energy conversion and destroy your home state, it's 
# NOT MY FAULT.

use Doctrine\Common\ClassLoader;
echo "Starting...<BR>\n";
require 'vendor/autoload.php';

$config = new \Doctrine\DBAL\Configuration();

# Note, when I created my testing database in dreamhost,
# for some reason it defaulted to utf8mb3
# This resulted in a cryptic error, which I'm afraid I didn't save.
# The fix was to alter my test database to utf8mb4 with this command:
# ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
$connectionParams = array(
                          'dbname' => 'INFORMATION_SCHEMA',
                          'user' => 'username',
                          'password' => 'password',
                          'host' => 'hostname.example..com',
                          'port' => 3306,
                          'charset' => 'utf8',
                          'driver' => 'pdo_mysql',
                          );
$dbh = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
echo "Querying table.<BR>\n";
$sth = $dbh->query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'databasename'");
echo "Iterating through tables:<BR>\n";
echo "<TABLE BORDER=1>\n";
$headers = 0;
while (($row = $sth->fetchAssociative()) !== false) {
#  echo(var_dump($row));
  if (!$headers) {
    echo "<TR>";
    foreach ($row as $key => $value) {
      echo "<TH>$key</TH>";
    }
    echo "</TR>\n" ;
    $headers = 1;
  }
  echo "<TR>";
  foreach ($row as $key => $value) {
    echo "<TD>$value</TD>";
  }
  echo "</TR>\n" ;
}
echo "</TABLE>" ;
echo "<HR>\n";
$sth = $dbh->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA LIKE 'databasename'");
echo "Iterating through columns:<BR>\n";
echo "<TABLE BORDER=1>\n";
$headers = 0;
while (($row = $sth->fetchAssociative()) !== false) {
#  echo(var_dump($row));
  if (!$headers) {
    echo "<TR>";
    foreach ($row as $key => $value) {
      echo "<TH>$key</TH>";
    }
    echo "</TR>\n" ;
    $headers = 1;
  }
  echo "<TR>";
  foreach ($row as $key => $value) {
    echo "<TD>$value</TD>";
  }
  echo "</TR>\n" ;
}
echo "</TABLE>" ;
echo "<HR>\n";
# Columns we care about are:
# TABLE_NAME
# COLUMN_NAME
# DATA_TYPE or COLUMN_TYPE
# CHARACTER_MAXIMUM_LENGTH
# IS_NULLABLE maybe
# COLUMN_DEFAULT
# COLUMN_KEY is PRI, UNI, or MUL
# re: COLUMN_KEY, see https://stackoverflow.com/questions/5317889/sql-keys-mul-vs-pri-vs-uni
# "a foreign key that references another table's primary key is MUL" (but it sounds like not always...)
# and https://stackoverflow.com/questions/5317889/sql-keys-mul-vs-pri-vs-uni/25101478#25101478
$sth = $dbh->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'databasename'");
while (($tablename = $sth->fetchOne()) !== false) {
  $sth_cols = $dbh->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'databasename'");
while (($tablename = $sth->fetchOne()) !== false) {
}
?>
Done!
</BODY>
</HTML>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM