2016-06-18 59 views
0

我在SQLite數據庫中有兩個表A和B.表A看起來是這樣的:在兩個SQLite表之間選擇n個最新行

| 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 | 

而表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 | 

而且我想要得到的兩個表之間的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但我得到一個空的結果(我的表是不是空的,雖然)。

我也看着聯接,但沒有設法得到任何工作。

有什麼想法?謝謝。

回答

0

使用union all

select * from(
select * from a 
union all 
select * from b 
) t 
order by created DESC LIMIT 4 
+0

我得到這個錯誤:'無法準備的語句:沒有這樣的列:即使'created'列不出兩個表中存在created',爲什麼呢? – au2001

+0

Wich列將使你從這個查詢中得到:'select * from union all select * from b' – RIKI

+0

哦,是的,我沒有在第二個'select'中選擇'created'列,我現在工作,謝謝 – au2001