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

后端 未结 2 612
北恋
北恋 2020-12-16 08:31

My question will use emails as an example, but this could apply to anything.


Normally before registering a new user (including inserting his/her email) I check

2条回答
  •  醉梦人生
    2020-12-16 09:04

    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

提交回复
热议问题