OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
Best method for checking if a row exists.

From: Nicholas Ring (Nicholas.Ringsmf.com.au)
Date: Fri Dec 12 2008 - 14:31:41 CST


Hello,

What is the best method to check if (one or more) row exists (note:
primary key is auto inc and table engine is InnoDB - but what if these
were not true) ?

1) SELECT * FROM <table> WHERE <condition>

Check to see if the result set is non-empty.

2) SELECT COUNT(*) AS cnt FROM <table> WHERE <condition>

Check to see if the field 'cnt' is non-zero.

3) SELECT <primary key> FROM <table> WHERE <condition> LIMIT 1

Check to see if the result set is non-empty.

4) SELECT 1 FROM <table> WHERE <condition> LIMIT 1

Check to see if the result set is non-empty.

Any other possibilities?

I hate (1) and don't mind the rest if PK is auto inc and table engine
is InnoDB but I think (4) would be the best where PK is not auto inc
and/or table engine is not InnoDB.

Comments?

Many thanks in advance,
Nick

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql