繁体   English   中英

从两个表的两列中提取值(Sqlite)

[英]Extract values from two columns of two tables (Sqlite)

我有两个单独的表,比如

tab.1    
col.1
1
2
3
tab.2
col.1 col.2
56    77
66    99
88    09

我希望结果获得第一个 tab.1 值和第二个 tab.2 col.1 值,如

1 56
2 66
3 88

Union all 放置第二个选项卡。 第一个选项卡后的值。 价值观,但我需要像上面提到的风格。

如果结果取决于 2 列值的数字顺序,则使用row_number()窗口函数:

select t1.col1 tab1col1, t2.col1 tab2col1
from (select col1, row_number() over (order by col1) rn from tab1) t1  
inner join (select col1, row_number() over (order by col1) rn from tab2) t2  
on t1.rn = t2.rn

请参阅演示
如果tab1col1的值实际上是: 1, 2, 3, ... 那么tab1不需要row_number()

select t1.col1 tab1col1, t2.col1 tab2col1
from tab1 t1  
inner join (select col1, row_number() over (order by col1) rn from tab2) t2  
on t1.col1 = t2.rn

请参阅演示
结果:

| tab1col1 | tab2col1 |
| -------- | -------- |
| 1        | 56       |
| 2        | 66       |
| 3        | 88       |

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM