To Find if a record exists or not in MySql

To Find if a record exists or not:

Normally we use : SELECT COUNT(*) FROM anj_table WHERE fileldName = ‘anj_field’

Instead use sub-query

SELECT EXISTS (SELECT 1 FROM `anj_table` WHERE fieldName = ‘anj-value’ )

It executes faster than SELECT COUNT(*) to get if a particular row exists or not.

Reason being sub-query stop executing when it finds very first match.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>