Delete many rows from a table using id in Mysql

前端 未结 9 1860
遇见更好的自我
遇见更好的自我 2021-01-30 15:38

I am a Linux admin with only basic knowledge in Mysql Queries

I want to delete many table entries which are ip address from my table using id,

相关标签:
9条回答
  • 2021-01-30 16:11

    how about using IN

    DELETE FROM tableName
    WHERE ID IN (1,2) -- add as many ID as you want.
    
    0 讨论(0)
  • 2021-01-30 16:15

    Something like this might make it a bit easier, you could obviously use a script to generate this, or even excel

    DELETE FROM tablename WHERE id IN (
    1,
    2,
    3,
    4,
    5,
    6
    );
    
    0 讨论(0)
  • 2021-01-30 16:24

    If you have some 'condition' in your data to figure out the 254 ids, you could use:

    delete from tablename
    where id in 
    (select id from tablename where <your-condition>)
    

    or simply:

    delete from tablename where <your-condition>
    

    Simply hard coding the 254 values of id column would be very tough in any case.

    0 讨论(0)
  • 2021-01-30 16:26

    if you need to keep only a few rows, consider

    DELETE FROM tablename WHERE id NOT IN (5,124,221);
    

    This will keep only some records and discard others.

    0 讨论(0)
  • 2021-01-30 16:28

    Hope it helps:

    DELETE FROM tablename 
    WHERE tablename.id = ANY (SELECT id FROM tablename WHERE id = id);
    
    0 讨论(0)
  • 2021-01-30 16:30

    The best way is to use IN statement :

    DELETE from tablename WHERE id IN (1,2,3,...,254);
    

    You can also use BETWEEN if you have consecutive IDs :

    DELETE from tablename WHERE id BETWEEN 1 AND 254;
    

    You can of course limit for some IDs using other WHERE clause :

    DELETE from tablename WHERE id BETWEEN 1 AND 254 AND id<>10;
    
    0 讨论(0)
提交回复
热议问题