views:

87

answers:

3

I have db with multiple entries.

I Google out something like this

SELECT COUNT(*), item_id, text, number FROM ads
GROUP BY item_id, text, number
HAVING COUNT(*)>1;

this select (I think) all my multiple entries, I use SQLyog, ... and there is no option to press button and delete all results of this query.

but even if I select all one by one and delete, I would also delete original one, right?

I basically want to keep all unique entries and keep one of multiple items.

simple example

('1' 'Novi Sad' '123'); ('1' 'Novi Sad' '123'); ('3' 'Beograd' '124');

I want to keep

('1' 'Novi Sad' '123'); ('3' 'Beograd' '124');

Thanks! btw, I know only basic mysql.

+1  A: 

When you do delete entries make sure to reset your ID increment

ALTER TABLE 'table_name' AUTO_INCREMENT = 1
Nathan Witt
yea, I always wonder how to set that, thanks
Nemanja
+1  A: 

Can you just copy, drop and delete?

CREATE TABLE Copy_Temp as
SELECT item_id, text, number 
FROM ads
GROUP BY item_id, text, number;

DROP Table ads;

RENAME TABLE Copy_Temp TO ads;
Nick Craver
A: 

Select all unique records into a temp table.
Delete all records from original table.
Insert all records from your temp table into original table.

z-boss
This will select uniqueSELECT COUNT(*), item_id, text, number FROM adsGROUP BY item_id, text, numberHAVING COUNT(*)>1;but, it will not select one item from multiple ones.
Nemanja
No, the query should be something like:SELECT DISTINCT item_id, text, number FROM adsActually, your item_id should really be primary key and not repeat.
z-boss