First, create a unique constraint on that column, to ensure that it'll never have more than one record with the same email address. If the application tries to insert a duplicate record, the insert query will fail.
Second, you'll probably want to use MySQL's INSERT IGNORE
to suppress the error when the application attempts to insert a duplicate record. Then you can do the two queries in the opposite order: first do the INSERT IGNORE
to create a record if there wasn't one already, and then SELECT
whatever info you need based on the email address. If there was already a record for the same email address, the INSERT IGNORE
will do nothing and the SELECT
will find that pre-existing record.