3

假设我们有以下 2 个数据库:

    DROP DATABASE IF EXISTS `adb`;
    CREATE DATABASE `adb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
    USE `adb`;

    CREATE TABLE IF NOT EXISTS `Login` (
       `ID` bigint(20) NOT NULL,
       `Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
       PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

    INSERT INTO `Login` (`ID`, `Login`) VALUES
     (1, '2012-11-09 11:18:29'),
     (2, '2012-12-22 21:48:48'),
     (3, '2013-01-01 12:39:22');



    DROP DATABASE IF EXISTS `bdat`;
    CREATE DATABASE `bdat` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
    USE `bdat`;

   CREATE TABLE IF NOT EXISTS `Login` (
     `ID` bigint(20) NOT NULL,
     `Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

   INSERT INTO `Login` (`ID`, `Login`) VALUES
   (1, '2011-11-09 15:15:15'),
   (2, '2012-12-22 13:08:18'),
   (3, '2010-02-11 17:00:02');

我们也有 2 个查询。

查询1是:

       SELECT table_schema AS "Database", round(sum(data_length+index_length)/1024/1024,4) AS "Size (MB)" FROM information_schema.tables GROUP BY table_schema;

查询2是:

       SELECT Max(Login) AS "Last Login" FROM Login

如何将两个查询组合在一起以获得以下结果?

     Database             Size (MB)   Last Login
     adb                   0.0020     2012-12-22 13:08:18
     bdat                  0.0020     2013-01-01 12:39:22
     information_schema    0.0078     NULL
     mysql                 0.6133     NULL
4

1 回答 1

0

如果你使用大于 5.0 的 Mysql 版本可以使用FEDERATED TABLES。

例如,在BDAT中为ADB创建一个FEDERATED TABLE,使用以下代码:

CREATE TABLE federated_Login (
       `ID` bigint(20) NOT NULL,
       `Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
       PRIMARY KEY (`ID`)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://root@remote_host:9306/federated/Login';

然后您可以使用federated_Login作为BDAT的本地表,以获取更多信息:

http://dev.mysql.com/doc/refman/5.0/es/federated-use.html

于 2013-10-10T00:19:37.573 回答