SQLite3神奇的UNION、UNION ALL与LIMIT组合

以此备忘:

xxxxxx@ubuntu:~/sqlite/SQLite-036ebf72_orig_3.18.2$ ./sqlite3 t.db
SQLite version 3.18.2 2017-06-17 09:59:36
Enter ".help" for usage hints.
sqlite> create table t1(id integer primary key autoincrement, data text);
sqlite> create table t2(id integer primary key autoincrement, data text);
sqlite> insert into t1(data) values('t1_d1');
sqlite> insert into t1(data) values('t1_d2');
sqlite> insert into t2(data) values('t2_d1');
sqlite> insert into t2(data) values('t2_d2');
sqlite> select * from t1 union select * from t2;
1|t1_d1
1|t2_d1
2|t1_d2
2|t2_d2
sqlite> select * from(select * from(select * from t1 limit 1) union select * from(select * from t2 limit 1));
1|t1_d1
1|t2_d1
sqlite> select * from(select * from(select * from t1 limit 1) union all select * from(select * from t2 limit 1));
1|t1_d1
sqlite>
sqlite>
sqlite> select * from(select * from(select * from t1 limit 2) union all select * from(select * from t2 limit 2));
1|t1_d1
2|t1_d2
sqlite> select * from(select * from(select * from t1) union all select * from(select * from t2 limit 2));
1|t1_d1
2|t1_d2
1|t2_d1
2|t2_d2
sqlite> select * from(select * from(select * from t1 limit 2) union all select * from(select * from t2));
1|t1_d1
2|t1_d2
sqlite> select * from(select * from(select * from t1 limit 2) union select * from(select * from t2 limit 2));
1|t1_d1
1|t2_d1
2|t1_d2
2|t2_d2
sqlite> select * from(select * from(select * from t1) union select * from(select * from t2 limit 2));
1|t1_d1
1|t2_d1
2|t1_d2
2|t2_d2
sqlite> select * from(select * from(select * from t1 limit 2) union select * from(select * from t2));
1|t1_d1
1|t2_d1
2|t1_d2
2|t2_d2
sqlite> 

备注:aHR0cCUzQS8vd3d3LmNuYmxvZ3MuY29tL3poaGQv

原文地址:https://www.cnblogs.com/zhhd/p/7689055.html