Mysql
 sql >> Base de données >  >> RDS >> Mysql

mysql s'il n'existe pas

Je ne suis en aucun cas un expert en bases de données, mais voici comment je procède.

http://www.somethinghitme. com/2010/05/06/mysql-stored-procedures-if-not-exists/

    DECLARE SomeId int;
    DECLARE CheckExists int;
    SET CheckExists = 0;  

    SELECT 1 INTO CheckExists from lookup_table WHERE someField = in_SomeParam LIMIT 1;   

    IF (CheckExists > 0) THEN
        SELECT id INTO SomeId FROM lookup_table WHERE someField = in_SomeParam;
    ELSE
        INSERT INTO lookup_table (someField ) VALUES(in_SomeParam);
        SELECT SomeId = LAST_INSERT_ID();
    END IF;