sql - 如果列值不存在于该列中,则查询以插入列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1880514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:42:36  来源:igfitidea点击:

sql - query to insert a column value if it does not exist in that column

sql

提问by sangeetha

sql - query to insert a column value if it does not exist in that column

sql - 如果列值不存在于该列中,则查询以插入列值

回答by Jonas Lincoln

Hm. Do you want a new row? In that case,

嗯。你想要一个新的行吗?在这种情况下,

 IF NOT EXISTS(SELECT 1 FROM emp WHERE fruits = 'mango')
    INSERT INTO emp (fruits) VALUES ('mango')

回答by valli

Two ways to do

两种做法

1.IF NOT EXISTS (SELECT fruit FROM emp WHERE fruit='mango') 
BEGIN 
INSERT INTO emp(fruit) Values('mango'); 
END 

2.INSERT INTO emp ('mango') SELECT distinct fruit FROM emp WHERE not exists (select fruit from emp as e Where emp.fruit = e.fruit);