Reputation: 1122
I'm having some trouble with this one thing I'm working on in php(though I don't think it matters much).
I've got a table called apples
. It has a column called good apples
that has values 0 or 1
. I want to do an sql query that I want to use to display good apples=1
out of total apples in that table. How do I count ALL and good apples=1
in a single query, if that's the way to do this?
Upvotes: 2
Views: 258
Reputation: 36
Try this...
SELECT COUNT(good_apples) AS goodApples
FROM apples WHERE good_apples = 1
UNION SELECT COUNT(*) AS totalApples FROM apples
Upvotes: 0
Reputation: 6351
You could do something like:
SELECT b_good_apple, COUNT(b_good_apple) AS apple_count FROM apples
GROUP BY b_good_apple;
so that you get the "good apple" and "bad apple" counts. Obviously, the sum of the two is your total.
Upvotes: 0
Reputation: 25745
You can always use MySQL conditional count.
SELECT
COUNT(goodapples) as totalCount,
COUNT(CASE WHEN apples.goodapples = 1 THEN 1 END)
AS
goodapplecount
FROM
apple;
Upvotes: 2
Reputation: 3678
I suppose this will do:
SELECT SUM(good_apples), count(good_apples) FROM apple_table
Upvotes: 0
Reputation: 8079
try this:
select sum(good_apples) as good_count, count(*) as all_count from apples
Upvotes: 1