Jump to content

PHP/MySQL


c_pattle

Recommended Posts

You can do it in one query with either of these methods:

 

SELECT (SELECT count(*) FROM reviews WHERE review_rating BETWEEN 0 AND 20) AS zero_to_twenty,
(SELECT count(*) FROM reviews WHERE review_rating BETWEEN 21 AND 40) AS twentyone_to_forty

 

But that's really 5 queries all stuck together into one.

 

SELECT SUM(CASE WHEN review_rating BETWEEN 0 AND 20 THEN 1 ELSE 0 END) AS zero_to_twenty,
SUM(CASE WHEN review_rating BETWEEN 21 AND 40 THEN 1 ELSE 0 END) AS twentyone_to_forty
FROM reviews

 

This method is truly one query, and pretty much guarantees you'll only look at the table once.

Link to comment
https://forums.phpfreaks.com/topic/228632-phpmysql/#findComment-1178873
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.