Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
sqlite删除列a和列b不在前n项中的所有结果_Sql_Sqlite - Fatal编程技术网

sqlite删除列a和列b不在前n项中的所有结果

sqlite删除列a和列b不在前n项中的所有结果,sql,sqlite,Sql,Sqlite,假设我有下表 a b c ----------- 1 1 5 1 2 3 4 1 2 1 2 4 4 2 10 我想删除前n行中没有一行在a和b中具有与该行相同的值的所有行 例如,各种n的结果表是 n=1 n=2 n=3 n=4 我一直在尝试使用not in或not exists来解决这一问题,但由于我对两个列的匹配感兴趣,而不仅仅是1或整个记录,因此我很难做到这一点。由于您没有定义特定的顺序,因此结果没有完全定义,但这取

假设我有下表

a    b    c
-----------
1    1    5
1    2    3
4    1    2
1    2    4
4    2    10
我想删除前n行中没有一行在a和b中具有与该行相同的值的所有行

例如,各种n的结果表是

n=1

n=2

n=3

n=4


我一直在尝试使用not in或not exists来解决这一问题,但由于我对两个列的匹配感兴趣,而不仅仅是1或整个记录,因此我很难做到这一点。

由于您没有定义特定的顺序,因此结果没有完全定义,但这取决于实现的任意选择,即在limit子句中首先计算哪些行。例如,不同的SQLite版本可能会给出不同的结果。话虽如此,我相信您需要以下查询:

select t1.* from table1 t1, 
(select distinct t2.a, t2.b from table1 t2 limit N) tabledist 
where t1.a=tabledist.a and t1.b=tabledist.b;
其中,应将N替换为所需的行数

编辑:因此,要直接从现有表中删除,您需要以下内容:

with toremove(a, b, c) as 
    (select * from table1 tt 
    EXCEPT select t1.* from table1 t1, 
    (select distinct t2.a, t2.b from table1 t2 limit N) tabledist 
    where t1.a=tabledist.a and t1.b=tabledist.b) 
delete from table1 where exists 
(select * from toremove 
where table1.a=toremove.a and table1.b=toremove.b and table1.c=toremove.c);

标准的顺序是什么?是n=ID吗?这是不相关的-我只是想得到一个较小的数据集合,使其更可行,但我需要将任何给定a的所有数据与b一起,以使其有效。非常感谢。不过,我认为在第二部分中包含c是没有必要的
a    b    c
-----------
1    1    5
1    2    3
4    1    2
1    2    4

// The fourth row has the same values in a and b as the second, so it is not deleted. The first 3 rows of course match themselves so are not deleted
a    b    c
-----------
1    1    5
1    2    3
4    1    2
1    2    4 

// The first 4 rows of course match themselves so are not deleted. The fifth row does not have the same value in both a and b as any of the first 4 rows, so is deleted.
select t1.* from table1 t1, 
(select distinct t2.a, t2.b from table1 t2 limit N) tabledist 
where t1.a=tabledist.a and t1.b=tabledist.b;
with toremove(a, b, c) as 
    (select * from table1 tt 
    EXCEPT select t1.* from table1 t1, 
    (select distinct t2.a, t2.b from table1 t2 limit N) tabledist 
    where t1.a=tabledist.a and t1.b=tabledist.b) 
delete from table1 where exists 
(select * from toremove 
where table1.a=toremove.a and table1.b=toremove.b and table1.c=toremove.c);