mysql – 试图获得已申请年龄范围工作的男/女的百分比

对于令人困惑的标题感到抱歉,我想要实现的是,通过下表获取工作的总申请:

CREATE TABLE IF NOT EXISTS `applications` (
  `application_id` int(11) NOT NULL AUTO_INCREMENT,
  `application_user` varchar(100) NOT NULL,
  `application_date` datetime NOT NULL,
  `application_job` int(11) NOT NULL,
  `application_status` varchar(10) DEFAULT 'pending',
  `application_enabled` int(2) NOT NULL DEFAULT '1',
  `application_resume` int(11) NOT NULL,
  `application_description` text NOT NULL,
  PRIMARY KEY (`application_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

为了获得他们的年龄,我将user_personal_information加入到application_user,因为他们是申请了工作的用户.我的查询:

SELECT count(*) as total,
       user_gender as gender,
       TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age,
       application_date
FROM applications
LEFT JOIN user_personal_information
    ON user_personal_information_user = application_user
WHERE application_job = ?

我的用户的user_gender表可以等于男性/女性和user_birthdate,在上面的语句中我将其转换为年龄.

我试图将所有应用程序分组,例如:

16 - 21
22 - 30
31 - 45
45 - 64
65+

那个年龄段的男性和女性百分比.要用于需要这样的数据的datachart:

 "dataProvider": [
 {
   "age": "85+",
   "male": 25, //
   "female": 25
 }, {
   "age": "80-54",
   "male": 25,//percentage
   "female": 25//percentage
}]

因此,从上面可以看出,25%的男性已经应用了85岁及以上的女性,25%的女性已经应用.你得到了要点,这就是我试图让我的select语句工作的方式.

这将创建一个如下图表:

《mysql – 试图获得已申请年龄范围工作的男/女的百分比》

所以,为了澄清,我想计算总申请数并计算出基于性别和年龄组的申请百分比.如何使用上面的select语句执行此操作?

最佳答案 一些嵌套的分组子查询可以为您完成.请注意,计算的百分比是针对性别的:

select a.age, a.gender, a.cnt, 100*a.cnt/b.sm as percentage from
    (
        SELECT user_gender as gender,
               TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age,
               count(*) as cnt,
        FROM applications
        LEFT JOIN user_personal_information
        ON user_personal_information_user = application_user
        WHERE application_job = ?
        GROUP BY user_gender, TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE())
    ) a,
    (
        SELECT TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age,
               count(*) as sm
        FROM applications
        LEFT JOIN user_personal_information
        ON user_personal_information_user = application_user
        WHERE application_job = ?
        GROUP BY TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE())
    ) b
    where a.age = b.age;

如果您正在寻找特定于总应用程序的百分比,您需要以下内容:

select a.age, 
       a.gender, 
       a.cnt, 
100*a.cnt/(
            select count(TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()))
            from
            applications 
            LEFT JOIN user_personal_information
            ON user_personal_information_user = application_user
            WHERE application_job = ?
         ) as percentage 
from
    (
        SELECT user_gender as gender,
               TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age,
               count(*) as cnt,
        FROM applications
        LEFT JOIN user_personal_information
        ON user_personal_information_user = application_user
        WHERE application_job = ?
        GROUP BY user_gender, TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE())
    ) a;
点赞