创建一个视图,其中每一行都来自不同的 table
Create a view where every line comes from a different table
我有 tables 用于多个 stations
,每个站的数据:timestamp
、error
等。每个 [=28= 中的总行数] 是该站的错误频率。站名在table name.
CREATE TABLE Station_00 (error INT, timestamp DATETIME);
INSERT INTO Station_00 VALUES (1, '2020/10/05 12-12-12'),(2,'2020/10/05 12-12-15'),(3,'2020/10/05 12-12-20'),(4,'2020/10/05 12-12-25'),(5,'2020/10/05 12-12-30'),(6,'2020/10/05 12-12-35'),(7,'2020/10/05 12-12-37'),(8,'2020/10/05 12-12-40');
CREATE TABLE Station_01 (error INT, timestamp DATETIME);
INSERT INTO Station_01 VALUES (1, '2020/10/05 12-14-12'),(2,'2020/10/05 12-14-15'),(3,'2020/10/05 12-14-20'),(4,'2020/10/05 12-14-25');
CREATE TABLE Station_02 (error INT, timestamp DATETIME);
INSERT INTO Station_02 VALUES (1, '2020/10/05 12-14-17'),(2,'2020/10/05 12-14-20'),(3,'2020/10/05 12-14-26'),(4,'2020/10/05 12-14-29'),(5,'2020/10/07 12-14-29');
CREATE TABLE Station_03 (error INT, timestamp DATETIME);
INSERT INTO Station_03 VALUES (1, '2020/10/05 12-17-12'),(2,'2020/10/05 12-17-15'),(3,'2020/10/07 12-14-20'),(4,'2020/10/07 12-14-25'),(5,'2020/10/07 12-14-30'),(6,'2020/10/07 12-16-25');
事件值是随机的,不一定像此处那样按升序排列。
我想创建一个 VIEW
,行数与站数和列数一样多 station
(每个 table 的名称)和 frequency
(数字每个 table 中的行数)。有什么方法可以在 SELECT
中做到这一点吗?
我想要这样的东西:
+---------+-----------+
| Station | Frequency |
+---------+-----------+
| 00 | 8 |
| 01 | 4 |
| 02 | 5 |
| 03 | 6 |
+---------+-----------+
您可以使用 union all
:
create view myview as
select '00' as station, count(*) as cnt from table_station00
union all select '01', count(*) from table_station01
union all select '02', count(*) from table_station02
但是请注意,所有这些 table 可能应该合并到一个 table 中,并增加一个代表车站的列。然后,你可以这样做:
select station, count(*) cnt from table_station;
我有 tables 用于多个 stations
,每个站的数据:timestamp
、error
等。每个 [=28= 中的总行数] 是该站的错误频率。站名在table name.
CREATE TABLE Station_00 (error INT, timestamp DATETIME);
INSERT INTO Station_00 VALUES (1, '2020/10/05 12-12-12'),(2,'2020/10/05 12-12-15'),(3,'2020/10/05 12-12-20'),(4,'2020/10/05 12-12-25'),(5,'2020/10/05 12-12-30'),(6,'2020/10/05 12-12-35'),(7,'2020/10/05 12-12-37'),(8,'2020/10/05 12-12-40');
CREATE TABLE Station_01 (error INT, timestamp DATETIME);
INSERT INTO Station_01 VALUES (1, '2020/10/05 12-14-12'),(2,'2020/10/05 12-14-15'),(3,'2020/10/05 12-14-20'),(4,'2020/10/05 12-14-25');
CREATE TABLE Station_02 (error INT, timestamp DATETIME);
INSERT INTO Station_02 VALUES (1, '2020/10/05 12-14-17'),(2,'2020/10/05 12-14-20'),(3,'2020/10/05 12-14-26'),(4,'2020/10/05 12-14-29'),(5,'2020/10/07 12-14-29');
CREATE TABLE Station_03 (error INT, timestamp DATETIME);
INSERT INTO Station_03 VALUES (1, '2020/10/05 12-17-12'),(2,'2020/10/05 12-17-15'),(3,'2020/10/07 12-14-20'),(4,'2020/10/07 12-14-25'),(5,'2020/10/07 12-14-30'),(6,'2020/10/07 12-16-25');
事件值是随机的,不一定像此处那样按升序排列。
我想创建一个 VIEW
,行数与站数和列数一样多 station
(每个 table 的名称)和 frequency
(数字每个 table 中的行数)。有什么方法可以在 SELECT
中做到这一点吗?
我想要这样的东西:
+---------+-----------+
| Station | Frequency |
+---------+-----------+
| 00 | 8 |
| 01 | 4 |
| 02 | 5 |
| 03 | 6 |
+---------+-----------+
您可以使用 union all
:
create view myview as
select '00' as station, count(*) as cnt from table_station00
union all select '01', count(*) from table_station01
union all select '02', count(*) from table_station02
但是请注意,所有这些 table 可能应该合并到一个 table 中,并增加一个代表车站的列。然后,你可以这样做:
select station, count(*) cnt from table_station;