Reputation: 879
How can I use If exist in this statement ?
select count(*) as found from names where myname = "Hulk"
which one will be better ?
Upvotes: 1
Views: 166
Reputation: 425421
This statement:
SELECT COUNT(*) AS found
FROM names
WHERE name = 'Hulk'
will return you the total number of records for 'Hulk'
This statement:
SELECT 1 AS found
WHERE EXISTS
(
SELECT NULL
FROM names
WHERE name = 'Hulk'
)
will return 1
if at least one record exists, otherwise it will return nothing.
If you just need to check that at least one record exists, the latter query is more efficient.
Upvotes: 2