Brug insert . . . select :
INSERT INTO USER (name, email)
SELECT 'John', 'example@sqldat.com'
WHERE NOT EXISTS
(SELECT id FROM USER WHERE email = 'example@sqldat.com');
Jeg ville skrive dette som:
INSERT INTO USER (name, email)
SELECT name, email
FROM (SELECT 'John' as name, 'example@sqldat.com' as email) t
WHERE NOT EXISTS (SELECT 1 FROM USER u WHERE u.email = t.email);
Men en bedre tilgang er nok bare at indsætte et unikt indeks, så databasen beskytter dataene:
create unique index idx_users_email on user(email);