如何找到mysql表中连续数据中的gap


如何定位到mysql表中连续数据中的gap

一张表 table1 中, 自增ID不连续, 出现 gap


SELECT
 CONCAT(z.expected, IF(z.got-1>z.expected, CONCAT(' thru ',z.got-1), '')) AS missing
FROM (
 SELECT
  @rownum:=@rownum+1 AS expected,
  IF(@rownum=`YourCol`, 0, @rownum:=`YourCol`) AS got
 FROM
  (SELECT @rownum:=0) AS a
  JOIN `YourTable`
  ORDER BY `YourCol`
 ) AS z
WHERE z.got!=0;

How to find gaps in sequential numbering in mysql?

Buy me a 肥仔水!