Mysql 试图获得申请某一年龄段工作的男女比例
很抱歉,这个标题让人困惑,我想实现的是,通过下表获得一份工作的全部申请:Mysql 试图获得申请某一年龄段工作的男女比例,mysql,sql,Mysql,Sql,很抱歉,这个标题让人困惑,我想实现的是,通过下表获得一份工作的全部申请: 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 N
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\u personal\u information
进入application\u 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\u gender
的我的用户表,它可以等于男性
/女性
和user\u生日
,在上述声明中,我将其转换为年龄
我正在尝试将年龄范围为的所有应用程序分组,例如:
16 - 21
22 - 30
31 - 45
45 - 64
65+
以及该年龄段的男性和女性百分比。要用于需要以下数据的数据图表:
"dataProvider": [
{
"age": "85+",
"male": 25, //
"female": 25
}, {
"age": "80-54",
"male": 25,//percentage
"female": 25//percentage
}]
综上所述,25%的男性申请了85岁及以上,25%的女性申请。你明白要点了,所以这就是我如何让我的select语句工作的
这将创建如下图表:
因此,我想澄清一下,我想计算一下申请总数,并根据性别和年龄组计算出申请的百分比。如何使用上面的select语句实现这一点?几个嵌套的GROUPBY子查询可以为您实现这一点。请注意,计算的百分比因性别而异:
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;
这应该可以做到: select sum(case when age between 16 and 21 and gender='male' then 1 end) as '[Male 16-21]', sum(case when age between 16 and 21 and gender='female' then 1 end) as 'Female [16-21]', sum(case when age between 22 and 30 and gender='male' then 1 end) as '[Male 22-30]', sum(case when age between 22 and 30 and gender='female' then 1 end) as '[Female 22-30]', sum(case when age between 31 and 45 and gender='male' then 1 end) as '[Male 31-45]', sum(case when age between 31 and 45 and gender='female' then 1 end) as '[Female 31-45]', sum(case when age between 46 and 64 and gender='male' then 1 end) as '[Male 46-64]', sum(case when age between 46 and 64 and gender='female' then 1 end) as '[Female 46-64]', sum(case when age > 64 and gender='male' then 1 end) as '[Male Over 64]', sum(case when age > 64 and gender='female' then 1 end) as '[Female Over 64]', sum(case when 1=1 and gender='female' then 1 end) as '[Male TOTAL]', sum(case when 1=1 and gender='male' then 1 end) as '[Female TOTAL]' FROM applications 挑选 总和(年龄在16至21岁之间且性别为“男性”的情况下,则1结束)为“[男性16-21]”, 总和(如果年龄在16至21岁之间,性别为“女性”,则1结束)为“女性[16-21]”, 总和(如果年龄在22-30岁之间,性别为“男性”,则以1结尾)为“[男性22-30]”, 总和(如果年龄在22-30岁之间,性别为“女性”,则1结束)为“[女性22-30]”, 总和(如果年龄在31-45岁之间,性别为“男性”,则以1结尾)为“[男性31-45]”, 总和(如果年龄在31-45岁之间,性别为“女性”,则以1结尾)为“[女性31-45]”, 总和(如年龄在46至64岁之间,性别为“男性”,则以1结尾)为“[男性46-64]”, 总和(如果年龄在46至64岁之间,性别为“女性”,则1结束)为“[女性46-64]”, 总和(当年龄>64岁且性别为“男性”时,则以1结尾)为“[男性超过64岁]”, 总和(当年龄>64岁且性别=女性时,则1结束)为“[女性超过64岁]”, 总和(当1=1且性别为女性时,则1结束)为“[男性总数]”, 总和(当1=1且性别=男性时,则为1结尾)为“[女性总数]” 来自应用程序
抱歉,我也试图获取百分比并显示年龄范围,即(40-50)需要使用
大小写,当语句对计算年龄进行限制时,将其限制为组(例如16-21等)&然后按此分组以及genderI选择嵌套两个group by,并查询其联接的损坏情况。我想它会工作的。我收到一个错误:
警告:PDOStatement::execute():SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第71行的C:\xampp\htdocs\job\application\model\AnalyticModel.php中第16行的“a”(选择TIMESTAMPDIFF(YEAR,user\u birthdate,CURDATE())AS)附近使用的正确语法
好吧,CURDATE()
之后缺少close paranthes!