开发者

How to skip duplicates using INSERT INTO in a better optimized way?

开发者 https://www.devze.com 2023-02-10 12:12 出处:网络
I\'d like to build as an experiment a sort of dictionary where any user can suggest new words. In order to avoid duplicates, I used to do a query SELECT that search for that word and if size is zero

I'd like to build as an experiment a sort of dictionary where any user can suggest new words.

In order to avoid duplicates, I used to do a query SELECT that search for that word and if size is zero then I do the INSERT INTO.

I feel this method works well only if you need to warn the user br lese, but in my case I want something faster and automated and silent.

The very first entry of the word (the very first time a user suggests that word) is going to be the ID of the page word so I don't want to use REPLACE.

I was wondering wheth开发者_JAVA技巧er using INSERT IGNORE can be the solution?


INSERT IGNORE will do the trick for you here. You just need to make sure you have a UNIQUE index defined on the column you don't want duplicated.

Another options is INSERT INTO ... ON DUPLICATE KEY UPDATE which won't insert the value again, but will allow you update other columns in that row. A counter or timestamp for example.


"INSERT INTO" to ignore later duplicates, or "INSERT INTO ... ON DUPLICATE KEY UPDATE" to take new fields from the later duplicates: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

0

精彩评论

暂无评论...
验证码 换一张
取 消