根据文件中的主键列表获取行的子集
Get subset of rows based on a list of primary keys in file
我在一个文本文件中有一个很大的(1000 多个)ID 列表。我想 return 我的数据库中与这些 ID 对应的行。我该怎么做?
我目前的方法是简单地将整个列表粘贴到一个巨大的 SQL 查询中,然后 运行 那个。好用,但我觉得一定有更好的办法。
随着值列表变得越来越大,更好的解决方案是将其加载到 table,然后您可以将其用于查询。在 MySQL 中,load data
statement 语法对此很方便。
考虑如下:
create temporary table all_ids (id int);
load data infile 'myfile.txt' into table all_ids;
create index idx_all_ids on all_ids(id); -- for performance
select t.*
from mytable t
where exists (select 1 from all_ids a where a.id = t.id)
load data
语法接受许多选项以适应输入文件的格式 - 您可以阅读文档以获取更多信息。
我在一个文本文件中有一个很大的(1000 多个)ID 列表。我想 return 我的数据库中与这些 ID 对应的行。我该怎么做?
我目前的方法是简单地将整个列表粘贴到一个巨大的 SQL 查询中,然后 运行 那个。好用,但我觉得一定有更好的办法。
随着值列表变得越来越大,更好的解决方案是将其加载到 table,然后您可以将其用于查询。在 MySQL 中,load data
statement 语法对此很方便。
考虑如下:
create temporary table all_ids (id int);
load data infile 'myfile.txt' into table all_ids;
create index idx_all_ids on all_ids(id); -- for performance
select t.*
from mytable t
where exists (select 1 from all_ids a where a.id = t.id)
load data
语法接受许多选项以适应输入文件的格式 - 您可以阅读文档以获取更多信息。