插入..如果不存在? [MYSQL] [英] Insert Into .. If Not Exist ? [MYSQL]
本文介绍了插入..如果不存在? [MYSQL]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果数据库中不存在数据,我试图插入数据。它不起作用。
谁能告诉我为什么?提前致谢
insert into otlike values ( 1452 , g, g, df, dfg, < span class =code-string> fg, 2014-09-16, dfg)选择 id ,workdate 来自 otlike 其中 不 存在(选择 * 来自 otlike 其中 id = 1452 和 workdate = 2014-09-16)
解决方案
< blockquote>试试这个
IF 不 存在(选择 * 来自 otlike 其中 id = 1452 和 workdate = 2014-09-16)
BEGIN
insert into otlike values ( 1452, g, g, df , dfg, fg, 2014-09-16 , dfg)
结束
祝你好运; - )
Just检查下面它是否包含您遇到的相同类型的问题。
http://stackoverflow.co m / questions / 3164505 / mysql-insert-record-if-not-exists-in-table [ ^ ]
希望它有所帮助。
你可以通过这两种方式实现
如果不存在方法(简单和推荐方式)
IF 不 存在(选择 * 来自 otlike 其中 id = 1452 和 workdate = 2014-09-16)
BEGIN
插入 进入 otlike 值( 1452 , g, g, df, dfg, fg, 2014-09-16, dfg)
END
使用Tepm变量
声明@Count int
设置@Count =从otlike中选择count(*),其中id =1452和workdate =2014-09-16
if(@Count = 0)
插入otlike值(1452,g ,g,df,dfg,fg,2014-09-16,dfg)
开始
结束
I am trying to insert data if it does not exist in the database. and its not working.
Can anyone tell me why? Thanks in advance
insert into otlike values ("1452","g","g","df","dfg","fg","2014-09-16","dfg") Select id, workdate From otlike Where not exists (select * from otlike where id= "1452" and workdate = "2014-09-16")
解决方案
try this
IF not exists (select * from otlike where id= "1452" and workdate = "2014-09-16") BEGIN insert into otlike values ("1452","g","g","df","dfg","fg","2014-09-16","dfg") END
good luck ;-)
Just check the below it is consisting of the same type of problem you are facing.
http://stackoverflow.com/questions/3164505/mysql-insert-record-if-not-exists-in-table[^]
Hope it helps.
you can achive by this two ways
If not Exists Method (Simple And Recomented way)
IF not exists (select * from otlike where id= "1452" and workdate = "2014-09-16") BEGIN insert into otlike values ("1452","g","g","df","dfg","fg","2014-09-16","dfg") END
Using Tepm Variables
Declare @Count int Set @Count = select count (*) from otlike where id= "1452" and workdate = "2014-09-16" if( @Count = 0 ) insert into otlike values ("1452","g","g","df","dfg","fg","2014-09-16","dfg") begin end
这篇关于插入..如果不存在? [MYSQL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文