初始化数据直接复制 执行
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for amysky_wx_member_month_integral
-- ----------------------------
DROP TABLE IF EXISTS `amysky_wx_member_month_integral`;
CREATE TABLE `amysky_wx_member_month_integral` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`create_date` datetime NOT NULL,
`modify_date` datetime NOT NULL,
`customer_service` varchar(255) DEFAULT NULL,
`integral` bigint(20) DEFAULT NULL,
`month_integral_day` datetime DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of amysky_wx_member_month_integral
-- ----------------------------
INSERT INTO `amysky_wx_member_month_integral` VALUES ('1', '2018-08-14 14:03:00', '2018-08-14 14:03:00', '18236782913', '273', '2018-07-14 14:03:00', '18401683822');
INSERT INTO `amysky_wx_member_month_integral` VALUES ('2', '2018-08-14 14:03:00', '2018-08-14 14:03:00', '18236782913', '0', '2018-07-14 14:03:00', '18515999013');
INSERT INTO `amysky_wx_member_month_integral` VALUES ('3', '2018-08-14 14:03:00', '2018-08-14 14:03:00', '18236782913', '2', '2018-07-14 14:03:00', '13853000760');
INSERT INTO `amysky_wx_member_month_integral` VALUES ('4', '2018-08-14 14:03:00', '2018-08-14 14:03:00', '[email protected]', '234', '2018-07-14 14:03:00', '18601097059');
INSERT INTO `amysky_wx_member_month_integral` VALUES ('5', '2018-08-14 15:40:11', '2018-08-14 15:40:08', '[email protected]', '234', '2018-07-14 15:40:23', '18601097052');
INSERT INTO `amysky_wx_member_month_integral` VALUES ('6', '2018-08-14 15:40:53', '2018-08-14 15:40:55', '18236782913', '500', '2018-08-14 15:41:03', '18601097051');
SQL语句
SELECT
b.*
FROM
amysky_wx_member_month_integral AS b,
(
SELECT
a.integral AS integral
FROM
amysky_wx_member_month_integral AS a
WHERE
DATE_FORMAT(
'2018-07-14 14:03:00', //这是我替换的变量
'%Y%m'
) = DATE_FORMAT(
a.month_integral_day,
'%Y%m'
)
GROUP BY
a.integral
ORDER BY
a.integral DESC
LIMIT 0,
3
) AS c
WHERE
b.integral = c.integral
AND DATE_FORMAT(
'2018-07-14 14:03:00', //这是我替换的变量
'%Y%m'
) = DATE_FORMAT(
b.month_integral_day,
'%Y%m'
)
ORDER BY
b.integral DESC
我的问题是:
计算会员排名每月都要选出前三名但是不计算 人数 只要分数达到就OK
大体思路:
1.先根据时间过滤一下选出符合的月份的数据
select * from amysky_wx_member_month_integral as a where DATE_FORMAT('2018-07-14 14:03:00','%Y%m') =DATE_FORMAT(a.month_integral_day,'%Y%m')
2.再根据 integral 字段分组排序 limit 0,3 获取最高的三个
select a.integral as integral from amysky_wx_member_month_integral as a where DATE_FORMAT('2018-07-14 14:03:00','%Y%m') =DATE_FORMAT(a.month_integral_day,'%Y%m') GROUP BY a.integral ORDER BY a.integral DESC LIMIT 0,3
3.使用临时表 查询
select b.* from amysky_wx_member_month_integral as b ,(select a.integral as integral from amysky_wx_member_month_integral as a where DATE_FORMAT('2018-07-14 14:03:00','%Y%m') =DATE_FORMAT(a.month_integral_day,'%Y%m') GROUP BY a.integral ORDER BY a.integral DESC LIMIT 0,3) as c where b.integral=c.integral and DATE_FORMAT('2018-07-14 14:03:00','%Y%m') =DATE_FORMAT(b.month_integral_day,'%Y%m') ORDER BY b.integral DESC
到这就是这样 ,也许还有其他的办法,只是没有想起来