我有A,B,C和D.SQLite的:如何與SQL結合的順序限制左連接查詢
我SQL1 SELECT * FROM main_table order by main_table.date desc limit 2
回報A和B.
我SQL2 SELECT * FROM main_table left join sub_table using (key) where sub_table._id is not null
返回B和C.
我想有一個返回A,B和C基本上,我想加入SQL1或SQL2
我怎麼能結合這兩SQLS(SQLite中)單個SQL語句optimall ÿ?
我的數據是如下
main_table
_id date key
1 2016-08-04 D
2 2016-10-06 A
3 2016-09-04 B
4 2016-07-05 C
sub_table
_id age key
1 8 B
2 9 C
慾望輸出
m._id m.date m.key s._id s.age s.key
2 2016-10-06 A
3 2016-09-04 B 1 8 B
4 2016-07-05 C 2 9 C
我的選擇的邏輯......我要挑前兩名最新數據,以及數據也有一個時代。其他人我不在乎(即D不在前2,並且沒有年齡)。
請提供一些示例數據和您所需的輸出。 (請參閱[如何 格式化SQL堆棧溢出 post?](http://meta.stackexchange.com/q/96125)如何添加一些。) –