SQL: Select Keys that doesn't exist in one table

前端 未结 6 747
刺人心
刺人心 2021-01-11 11:24

I got a table with a normal setup of auto inc. ids. Some of the rows have been deleted so the ID list could look something like this:

(1, 2, 3, 5, 8,

6条回答
  •  渐次进展
    2021-01-11 12:03

    We can use MYSQL not in option.

    SELECT id
    FROM table_one
    WHERE id NOT IN ( SELECT id FROM table_two )
    

    Edited

    If you are getting the source from a csv file then you can simply have to put these values directly like:

    I am assuming that the CSV are like 1,2,3,...,n

    SELECT id
    FROM table_one
    WHERE id NOT IN ( 1,2,3,...,n );
    

    EDIT 2

    Or If you want to select the other way around then you can use mysqlimport to import data in temporary table in MySQL Database and retrieve the result and delete the table.

    Like:

    Create table

    CREATE TABLE my_temp_table(
       ids INT,
    );
    

    load .csv file

    LOAD DATA LOCAL INFILE 'yourIDs.csv' INTO TABLE my_temp_table
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'
    (ids);
    

    Selecting records

    SELECT ids FROM my_temp_table
    WHERE ids NOT IN ( SELECT id FROM table_one )
    

    dropping table

    DROP TABLE IF EXISTS my_temp_table
    

提交回复
热议问题