155

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              2              1       2013
 1              2              1       2013
 2              2              1       2013
 1              3              1       2013

id_users and id_product are foreign keys from different tables.

What I want is to delete just one row:

1     2     1    2013

Which appears twice, so I just want to delete it.

I've tried this query:

delete from orders where id_users = 1 and id_product = 2

But it will delete both of them (since they are duplicated). Any hints on solving this problem?

6 Answers 6

210

Add a limit to the delete query

delete from orders 
where id_users = 1 and id_product = 2
limit 1
5
  • 3
    This only deletes one row. If there were 3 with this user and product id then 2 would remain.
    – Rob
    Commented Aug 22, 2013 at 10:48
  • 10
    Yes, OP say (s)he wants to delete 1 row. That is what my query does.
    – juergen d
    Commented Aug 22, 2013 at 10:49
  • 2
    Yes, but I think this is not what he/she wants.
    – Rob
    Commented Aug 22, 2013 at 10:50
  • 3
    What OP really needs to do is to check first what rows are duplicated then delete the duplicates.
    – wbinky
    Commented Jun 10, 2014 at 15:17
  • In case of more copies than two this works if limit 1 is modified to limit (select count(*)-1 from orders where [appropriate filters to select all duplicates]).
    – sisisisi
    Commented Oct 18, 2018 at 13:15
64

All tables should have a primary key (consisting of a single or multiple columns), duplicate rows doesn't make sense in a relational database. You can limit the number of delete rows using LIMIT though:

DELETE FROM orders WHERE id_users = 1 AND id_product = 2 LIMIT 1

But that just solves your current issue, you should definitely work on the bigger issue by defining primary keys.

2
  • 1
    This is good to mention that the primary key should be unique in a relational databse.
    – Paul
    Commented Feb 19, 2014 at 15:00
  • 1
    @Paul This is exactly what I about to say. But don't forget unique key doesn't necessary means primary key.
    – Ryan Fung
    Commented Oct 28, 2015 at 7:11
20

You need to specify the number of rows which should be deleted. In your case (and I assume that you only want to keep one) this can be done like this:

DELETE FROM your_table WHERE id_users=1 AND id_product=2
LIMIT (SELECT COUNT(*)-1 FROM your_table WHERE id_users=1 AND id_product=2)
2
  • Thank you Rob, but no... I just want to delete one single row, not to keep just one
    – Dani
    Commented Aug 22, 2013 at 11:00
  • This is not working for me in 5.5.40, it throws a syntax error. As mentioned here: stackoverflow.com/a/578926/1076075, it seems like we can't use a subquery to specify the value of a LIMIT clause. For anyone trying to do stuff this way, check this out: stackoverflow.com/q/578867/1076075 Commented Dec 4, 2014 at 7:52
8

Best way to design table is add one temporary row as auto increment and keep as primary key. So we can avoid such above issues.

1
  • 1
    Or a permanent row. I know this is an old question, but I though I should say it. Id's are used for this kind of situation. If you are worried about space: A BIGINT is only 8 bytes! Commented Jun 3, 2014 at 14:21
5

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;
1

You must add an id that auto-increment for each row, after that you can delet the row by its id. so your table will have an unique id for each row and the id_user, id_product ecc...

Not the answer you're looking for? Browse other questions tagged or ask your own question.