tags:

views:

401

answers:

3

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 somehting to do with the COUNT(*) function possibly?

+7  A: 
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.

altCognito
A: 
SELECT ID FROM TABLE WHERE ID = 'number'; 
SELECT count(*) FROM TABLE WHERE ID = 'number'; 1 - exists,

In your PHP or other code, you must check if one of these queries return value.

A: 

Just to add another example.

SELECT COUNT(id) FROM table WHERE id = 123
Ólafur Waage