【问题标题】:Extract values from two columns of two tables (Sqlite)从两个表的两列中提取值(Sqlite)
【发布时间】:2020-02-08 20:25:50
【问题描述】:

我有两个单独的表,比如

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

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

【问题讨论】:

  • 请在问题中包含两个表的架构。您需要一个内部连接,但在不了解更多上下文的情况下不清楚关键应该是什么。
  • 这两个表有关系吗?如果是,inner join 不是您需要的吗?
  • @JQSOFT 不,两个表不相关,我想要第二个选项卡。第一个前面的值。
  • @ybungalobill INNER JOIN 需要一些相关的标准。但我没有,想加入...
  • @Maya:你知道表格没有内在的顺序,对吧?

标签: sql sqlite join


【解决方案1】:

如果结果取决于 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

请参阅demo
如果tab1 中的col1 的值实际上是: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

请参阅demo
结果:

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

【讨论】:

  • 虽然你提供了演示但第一次查询给我语法错误。
  • [02:09:34] 在数据库“datamaster”上执行 SQL 查询时出错:靠近“(”:语法错误
  • 我不知道您为什么会收到此错误。可能你的 SQLite 版本早于 3.25.0,不支持窗口函数?
猜你喜欢
  • 1970-01-01
  • 2012-11-02
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多