/* Navicat Premium Data Transfer Source Server : 127.0.0.1 - root Source Server Type : MySQL Source Server Version : 80017 Source Host : 127.0.0.1:3306 Source Schema : mydb Target Server Type : MySQL Target Server Version : 80017 File Encoding : 65001 Date: 04/08/2021 15:32:57 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for crown -- ---------------------------- DROP TABLE IF EXISTS `crown`; CREATE TABLE `crown` ( `id` int(8) NOT NULL AUTO_INCREMENT, `team` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `years` int(8) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of crown -- ---------------------------- INSERT INTO `crown` VALUES (3, '活塞', 1990); INSERT INTO `crown` VALUES (4, '公牛', 1991); INSERT INTO `crown` VALUES (5, '公牛', 1992); INSERT INTO `crown` VALUES (6, '公牛', 1993); INSERT INTO `crown` VALUES (7, '火箭', 1994); INSERT INTO `crown` VALUES (8, '火箭', 1995); INSERT INTO `crown` VALUES (9, '公牛', 1996); INSERT INTO `crown` VALUES (10, '公牛', 1997); INSERT INTO `crown` VALUES (11, '公牛', 1998); INSERT INTO `crown` VALUES (12, '马刺', 1999); INSERT INTO `crown` VALUES (13, '湖人', 2000); INSERT INTO `crown` VALUES (14, '湖人', 2001); INSERT INTO `crown` VALUES (15, '湖人', 2002); INSERT INTO `crown` VALUES (16, '马刺', 2003); INSERT INTO `crown` VALUES (17, '活塞', 2004); INSERT INTO `crown` VALUES (18, '马刺', 2005); INSERT INTO `crown` VALUES (19, '热火', 2006); INSERT INTO `crown` VALUES (20, '马刺', 2007); INSERT INTO `crown` VALUES (21, '凯尔特人', 2008); INSERT INTO `crown` VALUES (22, '湖人', 2009); INSERT INTO `crown` VALUES (23, '湖人', 2010); SET FOREIGN_KEY_CHECKS = 1;
SELECT r.team, MIN(y1), MAX(y2) FROM (SELECT c1.`team`, c1.years AS y1, c2.`years` AS y2, c2.`years` - (@rowNum := @rowNum + 1) AS rownumflag FROM crown c1, crown c2, (SELECT (@rowNum := 0)) b WHERE c1.`years` + 1 = c2.`years` AND c1.`team` = c2.`team`) r GROUP BY r.team, r.rownumflag