当前位置:Gxlcms > PHP教程 > 一条SQL语句如何求环比!这个月简历数减去上个月的(如图)

一条SQL语句如何求环比!这个月简历数减去上个月的(如图)

时间:2021-07-01 10:21:17 帮助过:114人阅读

【这个月简历数】-【上个月的简历数】=【环比数】(如图)

sql语句如何写?
SELECT DATE_FORMAT(m_adddate,'%m') as month,count(m_id) as zong,DATE_FORMAT(m_adddate,'%Y') as year from job_myreceive where DATE_FORMAT(m_adddate,'%Y')=2011 GROUP BY DATE_FORMAT(m_adddate,'%m');


回复讨论(解决方案)

原来并不是有那么多人啊!

冒出个高手,帮忙一下

01月份怎么算上个月?

01月份怎么算上个月?

01就不用比了

只能纯sql计算?

只能纯sql计算? 结合php也行

如果用PHP就非常简单了,直接用sql语句有点繁琐,不过不是不可以!

mysql里面不是有减法么?

自连接当月与上月关联即可
DATE_FORMAT(m_adddate,'%Y%m') = DATE_FORMAT(DATE_SUB(m_adddate,INTERVAL 1 MONTH),'%Y%m')

自连接当月与上月关联即可
DATE_FORMAT(m_adddate,'%Y%m') = DATE_FORMAT(DATE_SUB(m_adddate,INTERVAL 1 MONTH),'%Y%m') 斑斑求详细,连接之后的语法 我贴进去 查询出:0

你给出测试数据

试试可不可以

select DATE_FORMAT(t1.m_adddate,'%m')  as month, t1.m_id,DATE_FORMAT(t1.m_adddate,'%Y') as year,t1.m_id-t2.m_id from job_myreceive t1 left join job_myreceive t2 on month(t1.m_adddate)-1=month(t2.m_adddate) where DATE_FORMAT(t1.m_adddate,'%Y')=2011

月份 简历数 年份 test(求环比)
01 4864 2011 0
02 8193 2011 0
03 7182 2011 0
04 8472 2011 0
05 7565 2011 0
06 7062 2011 0
07 5620 2011 0
08 5994 2011 0
09 5059 2011 0
10 5574 2011 0
11 3428 2011 0
12 5260 2011 0

【sql语句】:SELECT DATE_FORMAT(m_adddate,'%m') as month,count(m_id) as zong,DATE_FORMAT(m_adddate,'%Y') as year,
DATE_FORMAT(m_adddate,'%Y%m') = DATE_FORMAT(DATE_SUB(m_adddate,INTERVAL 1 MONTH),'%Y%m') as test
from job_myreceive where DATE_FORMAT(m_adddate,'%Y')=2011 GROUP BY DATE_FORMAT(m_adddate,'%m')

试试可不可以

select DATE_FORMAT(t1.m_adddate,'%m')  as month, t1.m_id,DATE_FORMAT(t1.m_adddate,'%Y') as year,t1.m_id-t2.m_id from job_myreceive t1 left join job_myreceive t2 on month(t1.m_adddate)-1=month(t2.m_adddate) where DATE_FORMAT(t1.m_adddate,'%Y')=2011
【不行哦 循环错的】

你给出测试数据 月份 简历数 年份 test(求环比)
01 4864 2011 0
02 8193 2011 0
03 7182 2011 0
04 8472 2011 0
05 7565 2011 0
06 7062 2011 0
07 5620 2011 0
08 5994 2011 0
09 5059 2011 0
10 5574 2011 0
11 3428 2011 0
12 5260 2011 0

【sql语句】:SELECT DATE_FORMAT(m_adddate,'%m') as month,count(m_id) as zong,DATE_FORMAT(m_adddate,'%Y') as year,
DATE_FORMAT(m_adddate,'%Y%m') = DATE_FORMAT(DATE_SUB(m_adddate,INTERVAL 1 MONTH),'%Y%m') as test
from job_myreceive where DATE_FORMAT(m_adddate,'%Y')=2011 GROUP BY DATE_FORMAT(m_adddate,'%m')

那你还要我建表录入数据吗?
你就不能导出sql指令?

那你还要我建表录入数据吗?
你就不能导出sql指令?


SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `job_myreceive`
-- ----------------------------
DROP TABLE IF EXISTS `job_myreceive`;
CREATE TABLE `job_myreceive` (
`m_id` int(10) unsigned NOT NULL auto_increment,
`m_rid` int(10) NOT NULL default '0',
`m_name` varchar(50) NOT NULL,
`m_sex` tinyint(1) NOT NULL default '0',
`m_birth` date NOT NULL,
`m_edu` tinyint(2) NOT NULL default '0',
`m_hid` int(10) NOT NULL default '0',
`m_place` varchar(50) NOT NULL COMMENT '应聘职位',
`m_cmember` varchar(20) NOT NULL,
`m_pmember` varchar(20) NOT NULL,
`m_adddate` datetime NOT NULL default '0000-00-00 00:00:00',
`m_read` tinyint(1) NOT NULL default '0',
`m_content` text NOT NULL,
`m_lang` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`m_id`),
KEY `m_place` (`m_place`),
KEY `m_hid` (`m_hid`)
) ENGINE=MyISAM AUTO_INCREMENT=111546 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records of job_myreceive
-- ----------------------------
INSERT INTO `job_myreceive` VALUES ('74', '625047', '李强', '1', '1980-01-01', '5', '251168', '助理医师', 'qq102971141', 'hzcxlgq', '2013-05-28 13:43:17', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('77', '625047', '李强', '1', '1980-01-01', '5', '245030', '住院医师', 'aabb41724125', 'hzcxlgq', '2010-12-28 13:54:27', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('78', '675931', '王医生', '1', '0000-01-01', '0', '271982', '肛肠科医师', 'zbyyyy', 'blair123', '2010-12-29 21:57:50', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('79', '691982', '谢勇访', '1', '1990-05-10', '1', '270201', '司机', 'persist', '司机', '2010-12-29 21:59:26', '0', '本司机带车想在门诊里面工作。联系电话 15817540656', '0');
INSERT INTO `job_myreceive` VALUES ('80', '691989', '林泽萍', '2', '1985-09-24', '5', '270166', '妇产科住院医师', 'dgsgyy', 'linzeping', '2010-12-29 22:00:36', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('82', '691989', '林泽萍', '2', '1985-09-24', '5', '265725', '妇产科', '6514585891AAA', 'linzeping', '2010-12-29 22:05:28', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('83', '687648', '林子航', '1', '1977-06-06', '5', '270969', '经营院长', 'xacayy', 'l608636', '2011-05-29 22:07:46', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('90', '687648', '林子航', '1', '1977-06-06', '5', '261560', '门诊主任', 'bagdnui', 'l608636', '2010-12-29 22:12:50', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('54523', '697204', '姚杰', '1', '1968-10-21', '6', '346100', '门诊主任', 'njcxyy', 'hbjzyaojie', '2011-07-16 13:29:51', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('92', '687648', '林子航', '1', '1977-06-06', '5', '271967', '门诊主任', '518fck', 'l608636', '2011-12-29 22:12:50', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('3232', '692140', '吕锋', '1', '0000-00-00', '5', '272140', '医院总经理', '遵义女子医院', '开拓发展', '2011-01-16 10:20:40', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('660', '692067', '卓志彬', '1', '1984-09-27', '4', '265632', '医院经营管理主任', 'yihao', '1291190', '2011-01-03 15:43:02', '0', '', '0');
INSERT INTO `job_myreceive` VALUES ('96', '650782', '张医生', '1', '0000-00-00', '6', '264340', '痔科', 'zg3610', 'yuanmeng', '2011-07-29 22:40:15', '0', '', '0');

SELECT DATE_FORMAT(a.m_adddate,'%Y%m') as Ym, count(*) as cnt_a,(select count(*) from job_myreceive where  DATE_FORMAT(a.m_adddate,'%Y%m') = DATE_FORMAT(DATE_SUB(m_adddate,INTERVAL 1 MONTH),'%Y%m')) as cnt_b FROM job_myreceive a group by 1
Ym     cnt_a  cnt_b  201012 6      2 201101 2      0 201105 1      0 201107 2      0 201112 1      0 201305 1      0 

在你给出的数据中
m_adddate 只有这些
'2010-12-29 22:12:50'
'2010-12-28 13:54:27'
'2010-12-29 21:57:50'
'2010-12-29 21:59:26'
'2010-12-29 22:00:36'
'2010-12-29 22:05:28'
'2011-01-16 10:20:40'
'2011-01-03 15:43:02'
'2011-05-29 22:07:46'
'2011-07-16 13:29:51'
'2011-07-29 22:40:15'
'2011-12-29 22:12:50'
'2013-05-28 13:43:17'

显然只有桃红的存在上月数据

嗯,这样写清楚些

SELECT DATE_FORMAT(a.m_adddate,'%Y%m') as Ym, count(*) as `当月`,(select count(*) from job_myreceive where  DATE_FORMAT(a.m_adddate,'%Y%m') = DATE_FORMAT(DATE_add(m_adddate,INTERVAL 1 MONTH),'%Y%m')) as 上月 FROM job_myreceive a group by 1
Ym     当月   上月  201012 6      0 201101 2      6 201105 1      0 201107 2      0 201112 1      0 201305 1      0

嗯,这样写清楚些

SELECT DATE_FORMAT(a.m_adddate,'%Y%m') as Ym, count(*) as `当月`,(select count(*) from job_myreceive where  DATE_FORMAT(a.m_adddate,'%Y%m') = DATE_FORMAT(DATE_add(m_adddate,INTERVAL 1 MONTH),'%Y%m')) as 上月 FROM job_myreceive a group by 1
Ym     当月   上月  201012 6      0 201101 2      6 201105 1      0 201107 2      0 201112 1      0 201305 1      0


感谢【xuzuning版主】的帮忙,实现了我的需求!

再请问一下这种2个select语句还有其他写法吗

人气教程排行