MySQL从自定义集中选择并与表数据进行比较
嗨,我正在尝试解决哪些元素在我的数据库中不存在。为此,我想将整数列表(外部脚本的输出)与表中的数据进行比较。如何做这样的事情:MySQL从自定义集中选择并与表数据进行比较,mysql,sql,Mysql,Sql,嗨,我正在尝试解决哪些元素在我的数据库中不存在。为此,我想将整数列表(外部脚本的输出)与表中的数据进行比较。如何做这样的事情: SELECT * FROM (1,1,2,3,5,8,13...) l WHERE l NOT IN (select id from table1); 这可能最好使用左外部联接来完成。但是,您的问题是创建常量表: SELECT * FROM (select 1 as id union all select 2 union all select 3 union all
SELECT * FROM (1,1,2,3,5,8,13...) l WHERE l NOT IN (select id from table1);
这可能最好使用
左外部联接来完成。但是,您的问题是创建常量表:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids
where ids.id NOT IN (select id from table1);
如果table1.id
曾经是NULL
,这可能会有奇怪的行为。以下工作更为普遍:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids left outer join
table1 t1
on ids.id = t1.id
where t1.id is null;
编辑:
MySQL查询的大小由参数max\u packet\u size
决定(请参阅)。最新版本的限制为1 GB。您应该能够容纳18000行:
select <n> union all
选择全部联合
很容易达到这个极限。天哪,我甚至不认为它会是1兆字节。不过,我要说的是,通过应用程序传递18000个ID的列表似乎效率低下。如果一个数据库可以直接从另一个数据库中提取数据,而不经过应用程序,那就太好了。这可能最好通过左外部联接来完成。但是,您的问题是创建常量表:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids
where ids.id NOT IN (select id from table1);
如果table1.id
曾经是NULL
,这可能会有奇怪的行为。以下工作更为普遍:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids left outer join
table1 t1
on ids.id = t1.id
where t1.id is null;
编辑:
MySQL查询的大小由参数max\u packet\u size
决定(请参阅)。最新版本的限制为1 GB。您应该能够容纳18000行:
select <n> union all
选择全部联合
很容易达到这个极限。天哪,我甚至不认为它会是1兆字节。不过,我要说的是,通过应用程序传递18000个ID的列表似乎效率低下。如果一个数据库可以直接从另一个数据库中提取数据,而不经过应用程序,那就太好了。如果要比较的数据集很大,我建议您创建一个临时表myid
,其中只包含id
,将所有18K值放在那里,然后像这样运行查询:
select id from myids where myids.id not in (select id from table1);
如果要比较的集合很大,我建议您创建一个临时表myid
,其中只包含一列id
,将所有18K值放在那里,然后像这样运行查询:
select id from myids where myids.id not in (select id from table1);
此列表包含大约18k个数字。。。做这样的事很难。@J33nn。然后将它们插入表中,并在查询中使用该表。或者,因为大多数长列表都是由查询生成的,所以只需将该查询用作子查询即可。查询是由外部脚本和其他数据库生成的。我想临时桌子是最好的方法。不过,如果能用更复杂的方法来做,那还是不错的。这个列表包含了大约18k的数字。。。做这样的事很难。@J33nn。然后将它们插入表中,并在查询中使用该表。或者,因为大多数长列表都是由查询生成的,所以只需将该查询用作子查询即可。查询是由外部脚本和其他数据库生成的。我想临时桌子是最好的方法。但是,用一种更复杂的方式来做这件事还是不错的。同意,但如何使它更普遍、更不残酷这是个问题:)同意,但如何使它更普遍、更不残酷这是个问题:)