How to insert a record to a table if there is no data exist of same type
insert into user_table (userid, active,contid) values ('AAA',1,1);
insert into user_table (userid, active,contid) values ('ABA',1,2);
INSERT INTO new_table(userid,isactive)
SELECT userid,1
FROM user_table where contid=1
WHERE NOT EXISTS (SELECT userid
FROM new_table
WHERE contid=1
)
i need to copy data from one table1 to table2 if table2 doesnt have the same data. if data exist just skip and dont make any insert. I am getting "SQL command not properly ended" error with the above query
>Solution :
The exisrs clause should have another condition.
You need to check if a active userid already exists too
INSERT INTO new_table(userid,isactive)
SELECT userid,1
FROM user_table WHERE contid=1
AND NOT EXISTS (SELECT 1
FROM new_table
WHERE user_table.userid = new_table.userid AND contid=1
)