sam sam - 4 years ago 97
MySQL Question

Insert into mysql table if nrecord not existed without using unique

I want to insert new record in database if not already present. I know I can do it if I make that column

unique
but cant do this as there are several redundant records already present . So i wish any new record I insert should only be inserted if not already present.

Sample table for reference

id name

1 a

2 b

3 c

Answer Source

Before inserting do a select query:

Select id from tablename where name = 'a' limit 1;

Then check, if the result has rows. If it does not have rows execute the insert statement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download