如果字段不包含子值,请更新

时间:2012-08-10 11:15:08

标签: mysql

我记录访问者的ip地址并将其存储到mysql数据库中。如果访问者获得不同的IP,它也将更新记录。我使用以下代码进行更新,以下示例中的值仅用于测试。

insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
                    ON DUPLICATE KEY UPDATE 
                    ip_address=concat(ip_address,'|','100.100.100.100'),
                    location=concat(location,'|','china'),
                    screen_res=concat(screen_res,'|','300x600'),
                    os=concat(os,'|','IOS'),
                    brower=concat(brower,'|','firefox')

它有效,但现在出现问题,如何检查数据库中是否有记录?像这样:访客再来,ip 100.100.100.100。 Mysql不知道有记录,它会重新记录。如何在插入之前检查是否包含子字符串?

1 个答案:

答案 0 :(得分:0)

    if not exists(select * from visiter_info where ip_address='100.100.100.100')
    insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','') 
ON DUPLICATE KEY UPDATE  
        ip_address=concat(ip_address,'|','100.100.100.100'), 
        location=concat(location,'|','china'), 
        screen_res=concat(screen_res,'|','300x600'), 
        os=concat(os,'|','IOS'), 
        brower=concat(brower,'|','firefox')