Boolean alternative to SELECT * FROM sometable WHERE condition

is there a Boolean alternative to SELECT * FROM sometable WHERE condition ? I only need to know if a row or several exists with that certain condition or not, no need to fetch the data. I want to do this to optimize my mysql queries. Is there such a way ?
iconpacificAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
SvenConnect With a Mentor Tech Lead Web-DevelopmentCommented:
SELECT RowCount = COUNT(*) FROM sometable WHERE condition

When RowCount > 0 then you have data inside!
0
 
hkamalCommented:
select CASE COUNT(*) when 0 then "False" else "True" end from TableName WHERE ColumnName=Condition

OR if you prefer 1,0 then

select CASE COUNT(*) when 0 then 0 else 1 end from TableName WHERE ColumnName=Condition
0
 
SvenTech Lead Web-DevelopmentCommented:
@hkamal: Would be no difference to mine! For example in VB:

If rs("rowcount") = 0 Then
Else
End If

Or yours:

If rs(0) = 0 Then
Else
End If

You see? No difference at all!
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
psadacCommented:
to DarthSonic :
do you really use mysql ? yes there is a big difference between your query and hkamal one : yours does not work.

RowCount = COUNT(*)     is incorrect

COUNT(*) AS RowCount   will work

0
 
snoyes_jwCommented:
You can also use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS(). If you put a LIMIT on the first query, you don't have to return the entire result set to the client.  I think the COUNT(*) option is probably easier in this case, though.

http://dev.mysql.com/doc/mysql/en/Information_functions.html
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
try this..

SELECT ifnull( count( * ) , NULL ) AS Total
FROM tableName
WHERE columnName = 'columnCondition'
0
 
SvenTech Lead Web-DevelopmentCommented:
@ushastry
isn't it:

SELECT isnull( count( * ) , 0) AS Total
FROM tableName
WHERE columnName = 'columnCondition'

First: isNull instead of ifNull (in MS SQL it is)
Second set to zero when NULL, otherwise it would be the same as before calling the function: null ;)

At all you do not need isNull, cause COUNT(*) will NEVER return NULL!
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
Hi DarthSonic,

Im not that much familiar with MS SQL.. but in mysql I think it is IFNULL.

0
 
SvenTech Lead Web-DevelopmentCommented:
never the less it is unnecessary to call this function as COUNT(*) will ever return zero if nothing is found ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.