Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查找同一表中两行之间的匹配列数据_Sql_Database_Sqlite - Fatal编程技术网

Sql 查找同一表中两行之间的匹配列数据

Sql 查找同一表中两行之间的匹配列数据,sql,database,sqlite,Sql,Database,Sqlite,我想在同一个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查询?我硬编码了行的值,因为我不知道如何在sq

我想在同一个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查询?

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

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
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

我硬编码了行的值,因为我不知道如何在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

我不明白你的配对是怎么回事。为什么第2行和第3行给出了9?很抱歉,第一列是rowid,并从比较中排除。您到底比较什么?任意两行之间的整数值。在本例中,包含integerI的col1、col2和col3我不理解匹配是如何工作的。为什么第2行和第3行给出了9?很抱歉,第一列是rowid,并从比较中排除。您到底比较什么?任意两行之间的整数值。在本例中,col1、col2和col3包含integerDecare?SQLite支持这一点吗?在我的表中,我有8列,所以这个查询比提取列值然后将它们分配给数组并使用sql之外的代码进行比较快吗?@PutraKg对于8列,您需要一个不同的脚本。答案已更改。谢谢t-clausen。所以男人们都很乐于助人。事实上,我是数据库编程sqlite的新手,所以希望您能容忍我:。我修改了脚本并执行了SQl。我得到了正确的结果。但是如果我只想一次比较任意两行呢?@PutraKg我修改了查询以比较rowid1和3decare?SQLite支持这一点吗?在我的表中,我有8列,所以这个查询比提取列值然后将它们分配给数组并使用sql之外的代码进行比较快吗?@PutraKg对于8列,您需要一个不同的脚本。答案已更改。谢谢t-clausen。所以男人们都很乐于助人。事实上,我是数据库编程sqlite的新手,所以希望您能容忍我:。我修改了脚本并执行了SQl。我得到了正确的结果。但是如果我只想一次比较任意两行呢?@PutraKg我修改了查询以比较rowid1和3