How to ‘insert if not exists’ in MySQL?

Written by leif. Posted in Mysql, PHP, Tips


there’s also INSERT … ON DUPLICATE KEY UPDATE syntax, you can find explanations on

Post from
18th October 2007

To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality.

There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT … ON DUPLICATE KEY UPDATE.Imagine we have a table:

CREATE TABLE `transcripts` (