Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a MySQL insert query that "inserts if not exists"?

INSERT INTO table('name') VALUES("abc") IF NOT EXISTS name='abc'

If abc doesn't exist in the name column, then insert it. How can I write that query?

like image 352
TIMEX Avatar asked Dec 31 '25 16:12

TIMEX


1 Answers

INSERT IGNORE INTO table(name) VALUES('abc')

This will ignore the value if it already exists. Like pjotr said, this will require name to be a unique index. Source

like image 197
christiaanderidder Avatar answered Jan 02 '26 09:01

christiaanderidder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!