View Single Post
  #3 (permalink)  
Old 04-17-2006, 06:54 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 92
ben has disabled reputation
Default MySQL answer. Re: Insert or update a record if it already exists?

MySQL has a special construct for this. Assume the 'username' column below is UNIQUE:

[sql]
INSERT INTO users (username, email) values ('Jo', 'jo@email.com')
ON DUPLICATE KEY UPDATE email = 'jo@email.com'
[/sql]

The 'ON DUPLICATE KEY' statement only works on PRIMARY KEY and UNIQUE columns.
Reply With Quote