How to delete a certain row from mysql table with same column values?

前端 未结 6 907
庸人自扰
庸人自扰 2020-12-07 10:37

I have a problem with my queries in MySQL. My table has 4 columns and it looks something like this:

id_users    id_product    quantity    date
 1                     


        
6条回答
  •  一整个雨季
    2020-12-07 11:00

    There are already answers for Deleting row by LIMIT. Ideally you should have primary key in your table. But if there is not.

    I will give other ways:

    1. By creating Unique index

    I see id_users and id_product should be unique in your example.

    ALTER IGNORE TABLE orders ADD UNIQUE INDEX unique_columns_index (id_users, id_product)
    

    These will delete duplicate rows with same data.

    But if you still get an error, even if you use IGNORE clause, try this:

    ALTER TABLE orders ENGINE MyISAM;
    ALTER IGNORE TABLE orders ADD UNIQUE INDEX unique_columns_index (id_users, id_product)
    ALTER TABLE orders ENGINE InnoDB; 
    
    1. By creating table again

    If there are multiple rows who have duplicate values, then you can also recreate table

    RENAME TABLE `orders` TO `orders2`;
    
    CREATE TABLE `orders` 
    SELECT * FROM `orders2` GROUP BY id_users, id_product;
    

提交回复
热议问题