Mysql
 sql >> Database >  >> RDS >> Mysql

Come trovare le lacune nella numerazione sequenziale in MySQL?

Aggiorna

ConfexianMJS ha fornito molto meglio risposta in termini di prestazioni.

La risposta (non il più veloce possibile)

Ecco la versione che funziona su tabelle di qualsiasi dimensione (non solo su 100 righe):

SELECT (t1.id + 1) as gap_starts_at, 
       (SELECT MIN(t3.id) -1 FROM arrc_vouchers t3 WHERE t3.id > t1.id) as gap_ends_at
FROM arrc_vouchers t1
WHERE NOT EXISTS (SELECT t2.id FROM arrc_vouchers t2 WHERE t2.id = t1.id + 1)
HAVING gap_ends_at IS NOT NULL
  • gap_starts_at - first id nel gap attuale
  • gap_ends_at - ultimo id nel gap attuale