有一个简单的技巧可以将连续的条目折叠成一个组。如果按(row_number-entry)进行分组,则连续的条目将在同一组中结束。这是一个演示我的意思的示例:
查询 :
SELECt phonenum, @curRow := @curRow + 1 AS row_number, phonenum - @curRowfrom phonenums pjoin (SELECt @curRow := 0) r
结果 :
| PHonENUM | ROW_NUMBER | PHonENUM - @CURROW |-------------------------------------------------| 27100070000 | 1 | 27100069999 || 27100070001 | 2 | 27100069999 || 27100070002 | 3 | 27100069999 || 27100070003 | 4 | 27100069999 || 27100070004 | 5 | 27100069999 || 27100070005 | 6 | 27100069999 || 27100070008 | 7 | 27100070001 || 27100070009 | 8 | 27100070001 || 27100070012 | 9 | 27100070003 || 27100070015 | 10 | 27100070005 || 27100070016 | 11 | 27100070005 || 27100070040 | 12 | 27100070028 |
请注意,连续的条目如何都具有相同的值
PHonENUM -@CURROW。如果我们在该列上进行分组,然后选择每个组的最小值和最大值,那么您将获得摘要(一个例外:如果需要,可以将
NULLSTART = END
替换为END值):
查询 :
select min(phonenum), max(phonenum) from( SELECt phonenum, @curRow := @curRow + 1 AS row_number from phonenums p join (SELECT @curRow := 0) r) pgroup by phonenum - row_number
结果 :
| MIN(PHONENUM) | MAX(PHONENUM) |---------------------------------| 27100070000 | 27100070005 || 27100070008 | 27100070009 || 27100070012 | 27100070012 || 27100070015 | 27100070016 || 27100070040 | 27100070040 |
演示:http ://www.sqlfiddle.com/#!2/
59b04/5



