SQLite创建视图以显示行中的表列

时间:2015-11-25 21:46:26

标签: sql sqlite views

我们说我有一张表:

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

1 个答案:

答案 0 :(得分:1)

您可以使用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;

SQLFiddleDemo