Usa insert . . . select
:
INSERT INTO USER (name, email)
SELECT 'John', '[email protected]'
WHERE NOT EXISTS
(SELECT id FROM USER WHERE email = '[email protected]');
Lo scriverei come:
INSERT INTO USER (name, email)
SELECT name, email
FROM (SELECT 'John' as name, '[email protected]' as email) t
WHERE NOT EXISTS (SELECT 1 FROM USER u WHERE u.email = t.email);
Ma un approccio migliore è probabilmente quello di inserire un indice univoco in modo che il database protegga i dati:
create unique index idx_users_email on user(email);