if not exists (select 1 from account where username='123') insert into account (username,passwd) values('123','123')

解决方案 »

  1.   

    如果表account的字段username没有值为123的数据,就插入一个username为123 passwd为123
      

  2.   

    晕啊,楼上的兄弟,意思我当然知道了,我问题是这条语句在MYSQL里面怎么写。
    我这样的写法在ms sql 2000里行得通,到MYSQL里就报错了
      

  3.   

    INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
        [INTO] tbl_name [(col_name,...)]
        SELECT ...
        [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
      

  4.   


    DELIMITER $$;DROP PROCEDURE IF EXISTS `emushu`.`sp_guests`$$CREATE PROCEDURE sp_test()
    BEGIN
    declare cnt int;
    select 1 from account where username='123' into cnt;
    if cnt != 0 then
     insert into account (username,passwd) values('123','123');
    end if;
    END$$DELIMITER ;$$
      

  5.   

    执行:
    call sp_guests();
      

  6.   


    DELIMITER $$;DROP PROCEDURE IF EXISTS sp_test$$CREATE PROCEDURE sp_test()
    BEGIN
    declare cnt int;
    select 1 from account where username='123' into cnt;
    if cnt != 0 then
    insert into account (username,passwd) values('123','123');
    end if;
    END$$DELIMITER ;$$