MySQL: Insert with conditional

1

I need to do the following situation, but when I run I get the error:

/ * SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 'INTO producto_seriais (serial_id) VALUES (         SELECT id FROM serial WHERE serial = 'at line 5 * /

SELECT CASE WHEN (
    SELECT COUNT(id) FROM seriais WHERE serial = '2020'
) > 1
THEN
    (INSERT INTO produto_seriais(serial_id) VALUES(
        SELECT id FROM seriais WHERE serial = '2020'
    ))
ELSE (
    INSERT INTO seriais (serial) VALUE('2020');
    SET @last_id_in_table1 = LAST_INSERT_ID();
    INSERT INTO produto_seriais (serial_id) VALUES (@last_id_in_table1);
)
END;

The case is as follows:

I'll look at the serial table with the serial "X". If it already exists, save its ID in the table "product_seriais". If it does not exist (the serial), I will save it, retrieve its ID and save it in "product_seriais". Any suggestions on how to do this?

Important note: this routine will run thousands of times with each run (10,000 or more, depending on the amount of serial).

    
asked by anonymous 17.10.2015 / 20:46

0 answers