Quickest way to check for pre-existing record before insert [mysql_errno()]

依然范特西╮ 提交于 2019-11-29 05:22:47

Use

INSERT IGNORE INTO Users VALUES(...);

with a unique key on email field, then check row count with mysql_affected_rows();

This will result in a single query to the DB and rule out the race condition of the time window between SELECT and INSERT

I believe that COUNT is one of the quickest methods. Also remember when doing your queries, if you only want to test its existence don't do a

SELECT *

Instead specify a specific field

SELECT id

So you are not retrieving as much data. In the case of using COUNT:

SELECT COUNT(id) FROM Users WHERE email = 'emailaddress'

Please see below link on how to use COUNT in a result set

http://www.tizag.com/mysqlTutorial/mysqlcount.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!