3

I have the following table named information.

enter image description here

As you see in the previous table there is column named number, this column has a many duplicate values.

What I want is, if the row is duplicate, update its tab cell to 1, otherwise, leave it as it is 0.

The following is what I done.

$query = mysql_query("UPDATE information SET tab='1' WHERE number = (SELECT distinct number FROM information)");
Lion King
  • 32,851
  • 25
  • 81
  • 143
  • 3
    Well, first try IN() instead of "=" (although there is considerable scope for further improvement) - and usual caveats about sql injection, deprecated functions, as well as the benefits of prepareed statements – Strawberry Feb 05 '14 at 12:07
  • Thanks about your warning. – Lion King Feb 05 '14 at 12:12

2 Answers2

4
UPDATE information
SET tab = '1'
WHERE number IN (SELECT number
                 FROM (SELECT number
                       FROM information
                       GROUP BY number
                       HAVING count(*) > 1
                      ) AS temp)

The subquery will return all duplicated number values.

Edit: I've tried it and MySQL shows 1093 error. I have just edited the query according to the solution found here.

Community
  • 1
  • 1
Iván Pérez
  • 2,278
  • 1
  • 24
  • 49
1

I would do this with an update and join:

UPDATE information i join
       (select number
        from information i
        group by number
        having count(*) > 1
       ) idups
       on i.number = idups.number
    SET i.tab = '1';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786