gpt4 book ai didi

MySQL查询具有相同表名和列名的所有数据库

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:24 24 4
gpt4 key购买 nike

假设我们有以下 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

最佳答案

如果使用大于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

关于MySQL查询具有相同表名和列名的所有数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com