Question: I have a table that keeps track of IP addresses with 3 fields -- ID, IP, TOTAL.

If I'm inserting a duplicate IP value, I'd like to just increment the TOTAL field by 1.

How should I do this?

Answer: 
You can use a query like this if your IP is UNIQUE

```
INSERT INTO tablename (id, ip, total) VALUES (1, xxx.xxx.x.x, 1)
ON DUPLICATE KEY UPDATE total = total+1;

```
**sp\_visitors.sql**

```
delimiter $$
create procedure sp_visitors (IN in_ip varchar(50))
begin
    declare var_total int;
    set var_total = (select total from ip_table where ip = in_ip);
    if (var_total is null) then
        insert into ip_table (ip, total) values (in_ip, 1);
    else
        update ip_table set total = total + 1 where ip = in_ip;
    end if;
end;
$$
delimiter ;

```

**grant privilege**

```
grant all on db.* to 'user'@'localhost';

```

**call**

```
call sp_visitors('192.168.168.1');

```