I have a table in which hundreds of thousands of integers will potentially be stored:
desc id_key_table; +----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | id_key | int(16) | NO | PRI | NULL | | +----------------+--------------+------+-----+---------+-------+
From the program, I have a large set of integers. I would like to see which of these integers is NOT in the id_key column above.
So far I have come up with the following approaches:
1) Pass through each integer and do:
select count(*) count from id_key_table where id_key = :id_key
When count is 0, id_key is not in the table.
It seems like a terrible, terrible way to do it.
2) Create a temporary table, insert each of the values ββinto the temporary table and execute JOIN in the two tables.
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 temp.id_key = main.id_key where main.killID is null; drop table id_key_table_temp;
This seems like a better approach, but I'm sure there is a much better approach that I haven't thought about yet. I would prefer not to create a temporary table and use a single query to determine which integers are missing.
Is there a valid query for this type of search?
(databases)