SQLite 创建视图以在行中显示 Table 列
SQLite Create View to Display Table Columns in Rows
假设我有一个 Table:
CREATE TABLE tbl_TableFullOfStuff (id INTEGER, Name STRING,
date1 INTEGER, date2 INTEGER);
里面有东西:
INSERT INTO tbl_TableFullOfStuff VALUES(1,"thing one", 1448486601, 1448486602);
INSERT INTO tbl_TableFullOfStuff VALUES(2,"thing two", 1448486605, 1448486606);
INSERT INTO tbl_TableFullOfStuff VALUES(3,"thing three", 1448486603, 1448486604);
如何创建一个视图,使每个时间戳成为独立的行?
NAME |TIMESTAMP
thing one |1448486601
thing one |1448486602
thing three |1448486603
thing three |1448486604
thing two |1448486605
thing two |1448486606
您可以使用 UNION ALL
:
CREATE VIEW my_view
AS
SELECT name, date1 AS TIMESTAMP
FROM tbl_TableFullOfStuff
UNION ALL
SELECT name, date2 AS TIMESTAMP
FROM tbl_TableFullOfStuff;
SELECT *
FROM my_view
ORDER BY name, TIMESTAMP;
假设我有一个 Table:
CREATE TABLE tbl_TableFullOfStuff (id INTEGER, Name STRING, date1 INTEGER, date2 INTEGER);
里面有东西:
INSERT INTO tbl_TableFullOfStuff VALUES(1,"thing one", 1448486601, 1448486602); INSERT INTO tbl_TableFullOfStuff VALUES(2,"thing two", 1448486605, 1448486606); INSERT INTO tbl_TableFullOfStuff VALUES(3,"thing three", 1448486603, 1448486604);
如何创建一个视图,使每个时间戳成为独立的行?
NAME |TIMESTAMP thing one |1448486601 thing one |1448486602 thing three |1448486603 thing three |1448486604 thing two |1448486605 thing two |1448486606
您可以使用 UNION ALL
:
CREATE VIEW my_view
AS
SELECT name, date1 AS TIMESTAMP
FROM tbl_TableFullOfStuff
UNION ALL
SELECT name, date2 AS TIMESTAMP
FROM tbl_TableFullOfStuff;
SELECT *
FROM my_view
ORDER BY name, TIMESTAMP;