Finding/searching for missing values in MySQL

前端 未结 5 1100
清歌不尽
清歌不尽 2021-01-16 14:04

I am using MySQL and have a table called sales. Its primary key is sales_id.

-------------------------------------
sales_id | invo         


        
5条回答
  •  感动是毒
    2021-01-16 14:32

    You can find gaps in a MySQL sequence using this query:

    SELECT invoice_id+1
       FROM sales s
       WHERE NOT EXISTS (
           SELECT NULL
           FROM sales t
           WHERE s.invoice_id = t.invoice_id+1
       ) 
       HAVING `invoice_id+1` < (SELECT MAX(invoice_id) FROM sales)
       ORDER BY invoice_id
    

    This will return all invoice_id missing from the sequence, regardless of sales_id.

提交回复
热议问题