Rick
asked on
Insert record into table (if record does not already exist)
Hello,
How do I insert a record into a column, if that record does not already exist.
If it does exist, then do nothing:
-------------------------- ---------- ---------- ---------- ---------- ----
INSERT INTO _table (_item) VALUES ('001') ..... ?
If _item 001 does not exist, then insert it, else do nothing.
-------------------------- ---------- ---------- ---------- ---------- ----
Please note that _item IS NOT a primary key, but I can't have duplicates there.
I'm using mysql 5.1.
Thanks,
Rick
How do I insert a record into a column, if that record does not already exist.
If it does exist, then do nothing:
--------------------------
INSERT INTO _table (_item) VALUES ('001') ..... ?
If _item 001 does not exist, then insert it, else do nothing.
--------------------------
Please note that _item IS NOT a primary key, but I can't have duplicates there.
I'm using mysql 5.1.
Thanks,
Rick
sory it will be NOT EXISTS...
IF NOT EXISTS (SELECT 1 FROM _table WHERE _item = '001')
INSERT INTO _table (_item) VALUES ('001')
IF NOT EXISTS (SELECT 1 FROM _table WHERE _item = '001')
INSERT INTO _table (_item) VALUES ('001')
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys.
IF EXISTS (SELECT 1 FROM _table WHERE _item = '001')
INSERT INTO _table (_item) VALUES ('001')