谁知道怎样判断表里面有没这条数据,如果没就添加有就更新
我在网上找了很多都不行我想可以再数据库里面运行sql但下面的老是不成功IF EXISTS (SELECT * FROM `ve123_links` WHERE `url`="xxxx") update `ve123_links` set `tuiguang`='1' where `url`='xxxx' ELSE BEGIN INSERT INTO `ve123_links`(`title`,`url`,`keywords`,`description`,`fulltxt`,`pic`,`nianfen`,`yuyan`,`diqu`,`zhuantai`) VALUES ('1','1','1','1','1','1','1','1','1','1') end
谁能矫正上面的语句

解决方案 »

  1.   

    MYSQL中比较简单,你可以直接用 replace 或者 insert into ... on duplicate update ..
    12.2.7. REPLACE Syntax
    REPLACE [LOW_PRIORITY | DELAYED]
        [INTO] tbl_name [(col_name,...)]
        {VALUES | VALUE} ({expr | DEFAULT},...),(...),...Or: REPLACE [LOW_PRIORITY | DELAYED]
        [INTO] tbl_name
        SET col_name={expr | DEFAULT}, ...Or: REPLACE [LOW_PRIORITY | DELAYED]
        [INTO] tbl_name [(col_name,...)]
        SELECT ...12.2.5.3. INSERT ... ON DUPLICATE KEY UPDATE Syntax
    If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect: INSERT INTO table (a,b,c) VALUES (1,2,3)
      ON DUPLICATE KEY UPDATE c=c+1;
      

  2.   

    如果表中有唯一索引 OR 主键,可以用REPLACE完成
      

  3.   

    如果表中没有唯一索引 OR 主键,用多条SQL语句完成
      

  4.   

    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
    免费手册中有完整的例子和说明。