mysql 實現DENSE_RANK

SELECT
	t.*, CASE
WHEN @integral = integral THEN
	@pm := @pm
ELSE
	@pm := @pm + 1
END pm,
 @integral := integral ints
FROM
	(
		(
			SELECT
				*
			FROM
				app_qixi_turntable
			WHERE
				`day` = '2017-05-15'
			ORDER BY
				integral DESC
		) t,
		(SELECT @pm := 0, @integral := 0) oth
	)

sql文件

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for app_qixi_turntable
-- ----------------------------
DROP TABLE IF EXISTS `app_qixi_turntable`;
CREATE TABLE `app_qixi_turntable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `integral` decimal(10,0) DEFAULT '0' COMMENT '積分',
  `day` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of app_qixi_turntable
-- ----------------------------
INSERT INTO `app_qixi_turntable` VALUES ('1', '24', '13430', '819', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('2', '1', '13430', '820', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('3', '2', '1343006', '820', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('4', '3', '13430', '825', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('5', '4', '1338', '826', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('6', '5', '134318', '825', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('7', '6', '134308', '829', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('8', '7', '134300', '821', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('9', '8', '1343008', '821', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('10', '9', '1343008', '821', '2017-05-15');
INSERT INTO `app_qixi_turntable` VALUES ('11', '23', '1348', '820', '2017-05-15');


發佈了28 篇原創文章 · 獲贊 10 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章