select 2 table 具有相同的列但没有连接

select 2 table with same column without join

我有 2 个 table 这样的

---------tbl1----------------
id  | name   | lname |
1      j          h
2      jj         hh  

---------tbl2----------------
_id  | name   | lname |
1        a         b
2        aa        bb
3        aaa       ccc

我想从这 2 table select 像这样:

---------result----------------
resultId   |  id  | name   | lname | 
   1           1      j         h
   2           2      jj        hh  
   3           1      a         b
   4           2      aa        bb
   5           3      aaa       ccc 

但是与select使用连接结果不是这种格式!我该怎么做才能获得这个 select 结果?

你基本上是在寻找union all:

select row_number() over (order by which, id) as resultid,
       id, name, lname
from ((select id, name, lname, 1 as which from tbl1
      ) union all
      (select _id, name, lname, 2 as which from tbl2
      )
     ) n
order by which, id;

唯一棘手的部分是使用 row_number() 来分配最终的 resultid