Mysql query, combine command about age and separate by gender

Codes here !

Moderators: egami, macek, gesf

Post Reply
nitiphone2021
php-forum Active User
php-forum Active User
Posts: 31
Joined: Fri Jan 08, 2021 8:10 pm

Sun Jan 17, 2021 9:28 pm

Dear friends,

I have a problem about my website and my website they have a lot of query information. so it make my website very slow.

I would like to make query command is shorter and faster as these 3 commands.

COMMAND1: show all people

COMMAND2: show only male

COMMAND3: show only female.

Any way I can combine them? Is it faster than old command?

Code: Select all

COMMAND 1
SELECT CASE WHEN `if_age` < 18 THEN 'under 18' 
WHEN `if_age` BETWEEN 18 and 29 THEN '18-29' WHEN `if_age` BETWEEN 30 and 39 THEN '30-39' 
WHEN `if_age` BETWEEN 40 and 49 THEN '40-49' WHEN `if_age` BETWEEN 50 and 59 THEN '50-59' 
WHEN `if_age` BETWEEN 60 and 69 THEN '60-69' WHEN `if_age` BETWEEN 70 and 79 THEN '70-79' 
WHEN `if_age` > 80 THEN 'Over 80' END as RANGE_AGE, count(`people_id`) as COUNT
FROM `tb_infected` WHERE tb_infected.qf_id = 1 GROUP BY RANGE_AGE ORDER BY RANGE_AGE

COMMAND2
SELECT CASE WHEN `if_age` < 18 THEN 'under 18' 
WHEN `if_age` BETWEEN 18 and 29 THEN '18-29' WHEN `if_age` BETWEEN 30 and 39 THEN '30-39' 
WHEN `if_age` BETWEEN 40 and 49 THEN '40-49' WHEN `if_age` BETWEEN 50 and 59 THEN '50-59' 
WHEN `if_age` BETWEEN 60 and 69 THEN '60-69' WHEN `if_age` BETWEEN 70 and 79 THEN '70-79' 
WHEN `if_age` > 80 THEN 'Over 80' END as RANGE_AGE, count(`people_id`) as COUNT
FROM `tb_infected` WHERE tb_infected.qf_id = 1 and if_gender = 0 GROUP BY RANGE_AGE ORDER BY RANGE_AGE

COMMAND3
SELECT CASE WHEN `if_age` < 18 THEN 'under 18' 
WHEN `if_age` BETWEEN 18 and 29 THEN '18-29' WHEN `if_age` BETWEEN 30 and 39 THEN '30-39' 
WHEN `if_age` BETWEEN 40 and 49 THEN '40-49' WHEN `if_age` BETWEEN 50 and 59 THEN '50-59' 
WHEN `if_age` BETWEEN 60 and 69 THEN '60-69'WHEN `if_age` BETWEEN 70 and 79 THEN '70-79' 
WHEN `if_age` > 80 THEN 'Over 80' END as RANGE_AGE, count(`people_id`) as COUNT
FROM `tb_infected` WHERE qf_id = 1 and if_gender = 1 GROUP BY RANGE_AGE ORDER BY RANGE_AGE
Post Reply