Jeg er på ingen måde en databaseekspert, men det er sådan jeg gør det.
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;