3

I've the following MySQL Table called store

id ref item_no supplier
1  10    x1      usa
2  10    x1      usa
3  11    x1      china
4  12    x2      uk
5  12    x3      uk
6  13    x3      uk
7  13    x3      uk

Now What i'm excepting the output to be is as follows :

id ref item_no supplier
1  10    x1      usa
3  11    x1      china
4  12    x2      uk
5  12    x3      uk
6  13    x3      uk

As you can see item_no x1 and x3 have same ref and supplier source, so what I want is to delete the duplicate record in-order to keep one item_no only !

I've create this PHP code to SELECT results only :

$query1 = "SELECT 
                DISTINCT(item_no) AS field, 
                COUNT(item_no) AS fieldCount, 
                COUNT(ref) AS refcount 
            FROM 
                store 
            GROUP BY item_no HAVING fieldCount > 1";

$result1 = mysql_query($query1);

if(mysql_num_rows($result1)>0){
    while ($row1=mysql_fetch_assoc($result1)) {
        echo $row1['field']."<br /><br />";
    }
} else {
    //IGNORE
}

How to tell the query to SELECT Duplicate records properly according to my needs before creating the DELETE query.

Thanks Guys

Ali Hamra
  • 232
  • 1
  • 8
  • 18
  • 1
    Possible duplicate of [Delete all Duplicate Rows except for One in MySQL?](http://stackoverflow.com/questions/4685173/delete-all-duplicate-rows-except-for-one-in-mysql) – Alex Andrei May 11 '16 at 12:44

3 Answers3

2

You can use the following query to produce the required result set:

SELECT t1.*
FROM store AS t1
JOIN (
   SELECT MIN(id) AS id, ref, item_no
   FROM store
   GROUP BY ref, item_no
) AS t2 ON t1.id > t2.id AND t1.ref = t2.ref AND t1.item_no = t2.item_no 

Demo here

To DELETE you can use:

DELETE t1
FROM store AS t1
JOIN (
   SELECT MIN(id) AS id, ref, item_no
   FROM store
   GROUP BY ref, item_no
) AS t2 ON t1.id > t2.id AND t1.ref = t2.ref AND t1.item_no = t2.item_no 
Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98
1

To find only duplicate records you can use

 SELECT * FROM store WHERE id NOT IN 
 (SELECT id FROM store AS outerStore WHERE id = 
 (SELECT MAX(id) FROM store AS innerStore 
 WHERE outerStore.ref = innerStore.ref AND 
 outerStore.supplier = innerStore.supplier AND outerStore.item_no = innerStore.item_no))

Maybe long, but it should work.

Vladislav Latish
  • 307
  • 1
  • 10
  • It doesn't matter if its long, the query itself is correct but it outputs empty records :(..I don't understand why – Ali Hamra May 11 '16 at 13:35
  • Ok I guess I found what is the problem..If I remove `outerStore.ref = innerStore.ref AND outerStore.supplier = innerStore.supplier AND` from your code, it works but not what I am expected...it only list duplicate items – Ali Hamra May 11 '16 at 13:39
  • It works in my local base with your data. `mysql_num_rows($result1)` return 0 ? – Vladislav Latish May 11 '16 at 13:44
0

If you want the select of the row to delete use

select *  from  store 
where id not in (
    select max(id)  from  store 
    group by  distinct ref, item_no, supplier);

Or you can directly use a command for direct delete using

delete from  store 
where id not in (
    select max(id)  from  store 
    group by  distinct ref, item_no, supplier);
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107