I'm trying to categorize reviews of a product to put in a chart. To summarize the results, I'm breaking the result set into four chucks:
Score >= 4.5
Score < 4.5 && Score >= 4
Score < 4 && Score >= 3
Score < 3
The SQL request can be seen below:
SELECT DISTINCT COUNT(*) AS cnt
FROM reviews
WHERE score >= 4.5
AND id = 4
AND live = '1'
UNION
SELECT DISTINCT COUNT(*) AS cnt
FROM reviews
WHERE score >= 4
AND score < 4.5
AND id = 4
AND live = '1'
UNION
SELECT DISTINCT COUNT(*) AS cnt
FROM reviews
WHERE score >= 3
AND score < 4
AND id = 4
AND live = '1'
UNION
SELECT DISTINCT COUNT(*) AS cnt
FROM reviews
WHERE score < 3
AND id = 4
AND live = '1'
This will then be put in an array. I've noticed the above request returns thirteen rows, or the summation of the counts equal 13. I get the below result:
10
2
1
Doing the below request...
SELECT score
FROM reviews
WHERE live = '1'
AND id = 4
...returns fourteen rows. I thought perhaps the result set might be causing issue, but I don't see any. The set of scores is as follows:
5.0, 5.0, 5.0, 5.0, 4.9, 4.9, 4.9
4.8, 4.8, 4.7, 4.4, 4.2, 3.4, 2.6
I'm not sure why that lone row is missing, any ideas, perhaps my syntax is wonky? Why is the final union (where score < 3
) return nothing?