如何查询数据库中的所有表以获取最新的现有值?

时间:2019-04-02 23:48:29

标签: mysql

我有几个数据库,其中每个表都包含来自电能表的数据,结构始终是这样的:

-------------------------------------------------------------------------
| dsid | tag | timestamp  | value   | startts    | correctts  | isfixed |
=========================================================================
|  1   | EE  | 1444716843 | 519.193 | 1444716000 | 1444716900 | 0       |
-------------------------------------------------------------------------
|  2   | PO  | 1444716843 | 0.090   | 1444716000 | 1444716900 | 0       |
-------------------------------------------------------------------------
|  3   | EE  | 1444717743 | 519.216 | 1444716900 | 1444717800 | 0       |
-------------------------------------------------------------------------

使用以下代码,我可以读取一个表中的最后一个现有值:

SELECT
  from_unixtime(TIMESTAMP) AS DATE,
  VALUE 
FROM `KA-AIK_Labor`
WHERE `timestamp` BETWEEN UNIX_TIMESTAMP('19-03-19') AND UNIX_TIMESTAMP('19-03-31 23:59:59')
ORDER BY DSID DESC LIMIT 1

我想实现的输出如下:

----------------------------------------------------------
| dsid | TABELNAME    | DATE                | VALUE      |
==========================================================
| 1    | KA-AIK_Labor | 2019-03-25 10:30:23 | 360884.000 |
----------------------------------------------------------
| 2    | KA-AIK_1. OG | 2019-03-25 10:44:00 | 12251.334  |
----------------------------------------------------------

我已经有了一些代码来获取数据库中所有表的列表:

select table_name from information_schema.tables where TABLE_SCHEMA='KAAIK'

但是凭我的一点SQL经验,我不知道如何将该代码合并到一个代码中。

此外,我喜欢将所有表格读成这样:

-------------------------------------------------------------------------------------
| dsid | DATE                | TABELNAME#1  | VALUE#1    | TABELNAME#1  | VALUE#1   |
=====================================================================================
| 1    | 2019-03-25 10:30:23 | KA-AIK_Labor | 360884.000 | KA-AIK_1. OG | 12251.315 |
-------------------------------------------------------------------------------------
| 2    | 2019-03-25 10:45:17 | KA-AIK_Labor | 360884.010 | KA-AIK_1. OG | 12251.325 |
-------------------------------------------------------------------------------------

我有以下代码:

SELECT
  from_unixtime(TIMESTAMP) AS DATE,
  VALUE 
FROM `KA-AIK_1. OG`
WHERE `timestamp` BETWEEN UNIX_TIMESTAMP('19-03-24') AND UNIX_TIMESTAMP('19-03-24 11:59:59')

阅读这样的列表:

-----------------------------------
| DATE                | VALUE     |
===================================
| 2019-03-25 10:30:23 | 12251.315 |
-----------------------------------
| 2019-03-25 10:45:17 | 12251.325 |
-----------------------------------

但是我仍然找不到如何转换/修改我的代码。

0 个答案:

没有答案