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
MySQL Server

Avatar of undefined
Last Comment
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')
Avatar of tigin44
tigin44
Flag of Türkiye image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Umesh
Umesh
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Rick
Rick

ASKER

Thanks guys.
MySQL Server
MySQL Server

MySQL is an open source, relational database management system that runs as a server providing multi-user access to a number of databases. Acquired by Oracle in 2009, it is frequently used in combination with PHP installations, powering most of the WordPress installations.

49K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo