Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends
I want to describe my problem there is a database table these table has some column I wanto to count datas...


id lesson Z K
1 x 0 0
2 x 0 0
3 x 2 1
4 x 2 1

only I will count when Z column has zero so this result will be 2
select count(x) from table where z=0 this result has 2

but when K column value is 1 I will add 2 datas so result will be 4

there is a condition here normally sql will count if z=0 so result 2
if k=1 sql count all of them so result will be 4

conditional count
how should be mysql query string?
thanks a lot.
Posted
Updated 5-Apr-13 1:49am
v2
Comments
kishore sharma 5-Apr-13 8:11am    
"when K column value is 1 I will add 2 datas so result will be 4"
what is the condition in query you want to check in this condition?
CPallini 5-Apr-13 8:20am    
You should state more clearly what you are trying to accomplish, in my opinion.

1 solution

It should works:
SQL
SELECT SUM(CountMyCol) AS SumOfZK
FROM(
    (SELECT COUNT(x) CountMyCol
    FROM MyTable
    WHERE z=0)
    UNION ALL
    (SELECT COUNT(x) CountMyCol
    FROM MyTable
    WHERE k=1))


More:
http://dev.mysql.com/doc/refman/5.0/en/union.html[^]
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900