Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if an ID exists in a database

Tags:

mysql

exists

When given an ID number, I want to check to see if it exists in the database. Return true if the ID is found and if not, then return false.

My knowledge of MySQL is very low, but I am assuming it would be something to do with the COUNT(*) function possibly?

like image 339
Ben McRae Avatar asked Apr 28 '09 13:04

Ben McRae


1 Answers

select id from table where id = $id

No need to get fancy. Using exists with subqueries seems likely only to generate poor performance, but I'm happy to be corrected if shown otherwise.

like image 159
cgp Avatar answered Oct 11 '22 00:10

cgp