Hi ! I need to have a single mysql query which should get COUNT of two different values in a single field, suppose i have a field in a table 'approved', and there are only two values ie. approved=0 or approved=1, and have suppose 100 rows for them,so how to get them counted like 0=20, 1=80, i have used distinct values query generated by PhpMyAdmin but i am not satisfied with it.
Thanks in advance !
faisal.qureshi.7121614
1
Newbie Poster
Recommended Answers
Jump to Postthe field name is currently "count(approved)" if you dumped your results thats what you would see
count(approved) as approved
would return approved as a field
Jump to PostHi you can use a condition statement inside your query that check whether your row contain the data that you wanna count..
SELECT COUNT(DISTINCT CASE WHEN approved = '0' THEN id END) 'zero', COUNT(DISTINCT CASE WHEN approved = '1' THEN id END) 'ones' FROM business_details
Jump to Posthi, you can try this
SELECT SUM(approved) `ones`, COUNT(*)-SUM(approved) 'zero' FROM `table`
All 9 Replies
cereal
1,524
Nearly a Senior Poster
Featured Poster
faisal.qureshi.7121614
1
Newbie Poster
jstfsklh211
79
Light Poster
faisal.qureshi.7121614
1
Newbie Poster
cereal
1,524
Nearly a Senior Poster
Featured Poster
faisal.qureshi.7121614
1
Newbie Poster
ehpratah
48
Posting Whiz in Training
catalinetu
1
Newbie Poster
faisal.qureshi.7121614
1
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.