insert . . . select
:
INSERT INTO USER (name, email)
SELECT 'John', '[email protected]'
WHERE NOT EXISTS
(SELECT id FROM USER WHERE email = '[email protected]');
मैं इसे इस प्रकार लिखूंगा:
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);
लेकिन एक बेहतर तरीका यह है कि केवल एक अद्वितीय अनुक्रमणिका डालें ताकि डेटाबेस डेटा की सुरक्षा कर सके:
create unique index idx_users_email on user(email);