Link to home
Start Free TrialLog in
Avatar of Rick
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
Avatar of tigin44
tigin44
Flag of Türkiye image



IF EXISTS (SELECT 1 FROM _table WHERE _item = '001')
      INSERT INTO _table (_item) VALUES ('001')
sory it will be NOT EXISTS...

IF NOT EXISTS (SELECT 1 FROM _table WHERE _item = '001')
      INSERT INTO _table (_item) VALUES ('001')
SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rick
Rick

ASKER

Thanks guys.