If there is no insert in MySql

This is MS SQL code

    if not exists (select PId from Person
             where Name = 'Name1' and Surname = 'Surname1')
    INSERT INTO [someDb].[dbo].[Person] 
        ([Name] ,[Surname])
    VALUES
        ('Name1' ,'Surname1')

Could you help me write ekvivalent code for my sql

thank

+3
source share
2 answers

Assuming you have a unique index (first name, last name), you can use INSERT IGNORE:

INSERT IGNORE INTO `someDb`.`Person` 
        (`Name` ,`Surname`)
    VALUES
        ('Name1' ,'Surname1')
+3
source

In MySQL , this is usually done. I usually did this before I saw another answer with

INSERT INTO table (fields) VALUES (values) ON DUPLICATE KEY UPDATE ID=ID;

In your case, columns (Name, Surname) will be required for the UNIQUE index

+1
source

Source: https://habr.com/ru/post/1785082/


All Articles