1

我想在同一个 sqlite 表中找到两行之间的匹配值。例如,如果我有下表:

rowid, col1, col2, col3
-----  ----  ----  ----
1      5     3     1    
2      3     6     9    
3      9     12    5

所以比较第 1 行和第 2 行,我得到值 3。

  • 第 2 行和第 3 行将给出 9。
  • 第 3 行和第 1 行将给出 5。

表中任意两行之间总会有一个且只有一个匹配值。

什么是正确的sqlite查询?

4

2 回答 2

1

我对行的值进行了硬编码,因为我不知道如何在 sqllite 中声明变量。

select t1.rowid as r1, t2.rowid as r2, t2.col as matchvalue from <yourtable> t1 join
(
  select rowid, col1 col from <yourtable> where rowid = 3 union all
  select rowid, col2 from <yourtable> where rowid = 3 union all
  select rowid, col3 from <yourtable> where rowid = 3
) t2
on t2.col in (t1.col1, t1.col2, t1.col3)
and t1.rowid < t2.rowid -- you don't need this if you have two specific rows
and t1.rowid = 1
于 2012-10-12T09:14:18.877 回答
0
select col from
(    
    select rid, c1 as col from yourtable
    union
    select rid, c2 from yourtable
    union
    select rid, c3 from yourtable
) v
where rid in (3,2)
group by col
order by COUNT(*) desc
limit 1
于 2012-10-12T09:09:36.037 回答