Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

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

like image 241
sangeetha Avatar asked Dec 10 '09 12:12

sangeetha


People also ask

How do you check if value in one column exists in another column in SQL?

Example using VLOOKUP You can check if the values in column A exist in column B using VLOOKUP. Select cell C2 by clicking on it. Insert the formula in “=IF(ISERROR(VLOOKUP(A2,$B$2:$B$1001,1,FALSE)),FALSE,TRUE)” the formula bar. Press Enter to assign the formula to C2.


2 Answers

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')
like image 85
Jonas Lincoln Avatar answered Oct 14 '22 22:10

Jonas Lincoln


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);
like image 35
valli Avatar answered Oct 14 '22 22:10

valli