【问题标题】:How to show all the tables from multiple databases如何显示来自多个数据库的所有表
【发布时间】:2010-06-12 07:22:14
【问题描述】:

如何在mySql中选择多个数据库中的所有表.. 我正在执行以下步骤,但无法实现目标。

<?php
$a = "SHOW DATABASES";
$da = $wpdb->get_results($a);

foreach($da as $k){
echo '<pre>';
print_r ($k->Database);//prints all the available databases
echo '</pre>';
$nq = "USE $k->Database";//trying to select the individual database
$newda = $wpdb->get_results($nq);
$alld = "SELECT * FROM $k->Database";
$td = $wpdb->get_results($alld);
var_dump($td);//returns empty array
}
?>

请帮帮我

【问题讨论】:

    标签: mysql mysql-management


    【解决方案1】:

    使用INFORMATION_SCHEMA

    select table_schema, table_name from information_schema.tables;
    

    【讨论】:

      【解决方案2】:

      更好:

      在一条 SQL 语句中显示所有数据库(内部 mysql 数据库除外)中的所有表。

      SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema NOT IN ( 'information_schema', 'performance_schema', 'mysql' )
      

      【讨论】:

      • 同时排除上面的'sys':...NOT IN (..., 'mysql', 'sys');
      【解决方案3】:

      你做不到

      SELECT * FROM database
      

      但你可以做到

      USE DATEBASE;
      SHOW TABLES;
      

      甚至更好:

      SHOW TABLES IN database
      

      【讨论】:

      • 您应该选择@cherouvim 的答案作为正确答案!
      • 这回答了原始发布者提出的另一个问题,它修复了他原本不正确的 sn-p 中的语法,因此它只是修复了他不正确的命令用法的语法。
      【解决方案4】:

      我会这样做:

      mysql -e "select table_schema, table_name from information_schema.tables;" | \
          grep -Pv '^(sys|performance_schema|TABLE_SCHEMA|mysql|information_schema)' | \
          perl -pe 's/\s+/./' | \
          sort -u
      

      【讨论】:

        【解决方案5】:

        mysql -e'select table_schema, table_name from information_schema.tables;'

        这取决于您是否有一个文件~/.my.cnf,其中包含以下内容:

        [client]
        user=ADMINUSER     ## set user, usually 'root' 
        password=PASSWORD  ## set password
        

        【讨论】:

          猜你喜欢
          • 2015-09-20
          • 2014-04-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-25
          • 2011-04-28
          • 2015-04-25
          • 1970-01-01
          相关资源
          最近更新 更多