Is it possible to insert a row but only if a value does not already exist?

前端 未结 7 1836
谎友^
谎友^ 2021-02-04 05:26

Is it possible to insert a row, but only if one of the values already in the table does not exist?

I\'m creating a Tell A Friend with referral points for an eco

7条回答
  •  情书的邮戳
    2021-02-04 05:37

    This works if you have a unique index or primary key on the column (EmailAddr in this example):

    INSERT IGNORE INTO Table (EmailAddr) VALUES ('test@test.com')
    

    Using this if a record with that email already exists (duplicate key violation) instead of an error, the statement just fails and nothing is inserted.

    See the MySql docs for more information.

提交回复
热议问题