0

say you want to check if any rows match a query (just a true/false if the table contains any matches). which is preferred (and optionally, why)? or is there a better way?

SELECT COUNT(*) > 0 FROM someTable WHERE someField = someValue

or

SELECT someField = someValue FROM someTable WHERE someField = someValue LIMIT 1

or

SELECT COUNT(*) > 0 FROM (SELECT someValue FROM someTable WHERE someField = someValue LIMIT 1) someAlias;
momo
  • 3,885
  • 4
  • 33
  • 54
  • http://stackoverflow.com/questions/4484974/the-quick-way-to-check-if-select-exists-returns-a-value-using-php – ethrbunny Dec 15 '12 at 16:04

1 Answers1

0

I would use EXISTS:

SELECT EXISTS (SELECT * FROM someTable);

This returns 1 (True) if the subquery returns any rows at all, otherwise it returns 0 (False).

Jeff H
  • 138
  • 1
  • 6