如果不存在,则在 MySql 中插入 [英] if not exists insert in MySql

查看:32
本文介绍了如果不存在,则在 MySql 中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 MS SQL 代码

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')

你能帮我为我的 sql 编写 ekvivalent 代码吗

can you please help me to write ekvivalent code in for my sql

谢谢

推荐答案

假设你在 (name,surname) 上有一个唯一索引,你可以使用 INSERT IGNORE:

Assuming you have a unique index on (name,surname), you can use INSERT IGNORE:

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

这篇关于如果不存在,则在 MySql 中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆