在多个表上查询具有不同值Mysql的相同表

时间:2018-05-10 09:31:57

标签: mysql

我需要一些mysql的帮助,因为我是新手。我需要计算来自不同数据库的不同表的值,并将它们全部放在一个表中,然后为每个计数生成一个索引,例如:

 _______________________
|           |           |
|  Indexes  |   Counts  |
|___________|___________|
|           |           |
|  Index 1  |  Count 1  |
|___________|___________|
|           |           |
|  Index 2  |  Count 2  |
|___________|___________|
|           |           |
|  Index 3  |  Count 3  |
|___________|___________|
|           |           |
|  Index 4  |  Count 4  |
|___________|___________|

但就像我现在这样做(使用UNION),它看起来像是:

 ___________
|           |
|  Counts   |
|___________|
|           |
|  Count 1  |
|___________|
|           |
|  Count 2  |
|___________|
|           |
|  Count 3  |
|___________|
|           |
|  Count 4  |
|___________|

有什么想法吗?提前谢谢。

编辑:

我目前使用的代码是

SELECT COUNT(*) AS "db1" FROM `db1`.`tb1`
UNION
SELECT COUNT(*) AS "db2" FROM `db2`.`tb2`

1 个答案:

答案 0 :(得分:0)

试试这个。

SELECT "db1.tb1" as "INDEXES", COUNT(*) AS "COUNTS" FROM `db1`.`tb1`
UNION
SELECT "db2.tb2" as "INDEXES", COUNT(*) AS "COUNTS" FROM `db2`.`tb2`