我有一个表,它可能会存储数十万个整数
desc id_key_table;
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| id_key | int(16) | NO | PRI | NULL | |
+----------------+--------------+------+-----+---------+-------+
Run Code Online (Sandbox Code Playgroud)
从程序中,我有一大组整数。我想看看这些整数中哪些不在上面的 id_key 列中。
到目前为止,我提出了以下方法:
1) 遍历每个整数并执行:
select count(*) count from id_key_table where id_key = :id_key
Run Code Online (Sandbox Code Playgroud)
当 count 为 0 时,表中缺少 id_key。
这似乎是一种可怕的、可怕的方式来做到这一点。
2) 创建一个临时表,将每个值插入临时表中,并对两个表执行JOIN。
create temporary table id_key_table_temp (id_key int(16) primary key );
insert into id_key_table_temp values (1),(2),(3),...,(500),(501);
select temp.id_key
from id_key_table_temp temp left join id_key_table as main
on …
Run Code Online (Sandbox Code Playgroud) mysql ×1