要確定的東西並不在該表中,您可以LEFT JOIN
到該表和測試結果是否IS NULL
。例如,給定任何(模型,序列)對,您可以構建預期遵循給定序列的(模型,序列)對。這是相同的型號,序列號較大。然後LEFT JOIN
該表與自己的條件是(模型,序列)對等於預期的。對於任何缺少的序列號,自加入別名IS NULL
中的每個列,因此只添加一個WHERE
條件,只保留那些行。
以下內容將幫助您入門。它確定了每個差距的開始,但不是所有差距中的數字。
-- List all rows in table serials
-- 1. which aren't the highest for a given SKU, and
-- 2. for which the following serial number doesn't exist for the same SKU
SELECT ser1.sku, ser1.serial_num + 1 AS serial_num
FROM serials AS ser1
INNER JOIN (
SELECT sku, MAX(serial_num) AS serial_num
FROM serials
GROUP BY sku
) AS sermax
ON (ser1.sku = sermax.sku
AND ser1.serial_num < sermax.serial_num)
LEFT JOIN serials AS ser2
ON (ser1.sku = ser2.sku
AND ser1.serial_num + 1 = ser2.serial_num)
WHERE ser2.serial_num IS NULL
輸出:
sku|serial_num
123| 3
345| 102
代碼在這個答案是雙重許可:CC BY-SA 3.0或MIT License as published by OSI。
那麼,您嘗試過哪些SQL /代碼? – user3791372
你會怎樣處理丟失的數字 – Strawberry