Select 两个 SQLite 表之间的 n 个最新行
Select n latest rows between two SQLite tables
我在 SQLite 数据库中有两个 tables A 和 B。 Table A 看起来像这样:
| id | name | description | created |
|------|---------------|---------------|--------------|
| 1 | "Something" | "Something" | 1466266963 |
| 2 | "Something" | "Something" | 1466266965 |
| 3 | "Something" | "Something" | 1466266967 |
| 4 | "Something" | "Something" | 1466266969 |
| 5 | "Something" | "Something" | 1466266971 |
| 6 | "Something" | "Something" | 1466266975 |
而 table B 是这样的:
| id | title | content | created |
|------|---------------|---------------|--------------|
| 1 | "Something" | "Something" | 1466266951 |
| 2 | "Something" | "Something" | 1466266953 |
| 3 | "Something" | "Something" | 1466266954 |
| 4 | "Something" | "Something" | 1466266956 |
| 5 | "Something" | "Something" | 1466266957 |
| 6 | "Something" | "Something" | 1466266978 |
而且我想获得两个 table 中最老的 4 个。
所以在这个例子中我会得到类似的东西:
| id | title | name | content | description | created |
|------|---------------|---------------|---------------|---------------|--------------|
| 6 | "Something" | | "Something" | | 1466266975 |
| 6 | | "Something" | | "Something" | 1466266975 |
| 5 | | "Something" | | "Something" | 1466266975 |
| 4 | | "Something" | | "Something" | 1466266975 |
我试过 SELECT A.*, B.* FROM A, B ORDER BY created DESC LIMIT 4
但我得到的结果是空的(不过我的 table 不是空的)。
我也查看了 JOIN,但没有成功。
有什么想法吗?谢谢。
使用union all
select * from(
select * from a
union all
select * from b
) t
order by created DESC LIMIT 4
我在 SQLite 数据库中有两个 tables A 和 B。 Table A 看起来像这样:
| id | name | description | created |
|------|---------------|---------------|--------------|
| 1 | "Something" | "Something" | 1466266963 |
| 2 | "Something" | "Something" | 1466266965 |
| 3 | "Something" | "Something" | 1466266967 |
| 4 | "Something" | "Something" | 1466266969 |
| 5 | "Something" | "Something" | 1466266971 |
| 6 | "Something" | "Something" | 1466266975 |
而 table B 是这样的:
| id | title | content | created |
|------|---------------|---------------|--------------|
| 1 | "Something" | "Something" | 1466266951 |
| 2 | "Something" | "Something" | 1466266953 |
| 3 | "Something" | "Something" | 1466266954 |
| 4 | "Something" | "Something" | 1466266956 |
| 5 | "Something" | "Something" | 1466266957 |
| 6 | "Something" | "Something" | 1466266978 |
而且我想获得两个 table 中最老的 4 个。
所以在这个例子中我会得到类似的东西:
| id | title | name | content | description | created |
|------|---------------|---------------|---------------|---------------|--------------|
| 6 | "Something" | | "Something" | | 1466266975 |
| 6 | | "Something" | | "Something" | 1466266975 |
| 5 | | "Something" | | "Something" | 1466266975 |
| 4 | | "Something" | | "Something" | 1466266975 |
我试过 SELECT A.*, B.* FROM A, B ORDER BY created DESC LIMIT 4
但我得到的结果是空的(不过我的 table 不是空的)。
我也查看了 JOIN,但没有成功。
有什么想法吗?谢谢。
使用union all
select * from(
select * from a
union all
select * from b
) t
order by created DESC LIMIT 4