简体   繁体   中英

How to list missing numerical values using SQL

i'm trying to figure out an SQL request for the API i'm developping.

So we have this "Paddle" table where the user registers and the api must provide a possible number for the paddle (simply a functionnal ID). Considering that the user can specify his own paddle number, i need to create a request that picks up every missing value.

I already have an sql request for that :

select MIN(p.number)+1 FROM Paddle p WHERE not exists(
SELECT pa.number
FROM Paddle pa
WHERE pa.channel = 'ABSENTEE'
      AND pa.sale_id = 3
      AND p.number + 1 = pa.number) AND p.channel = 'ABSENTEE' AND p.sale_id = 3);

But it can't detect when there is no paddle number 1.

The channel and sale_id are to restrict the numbers (i can have two paddle with 1 as number but not for the same sale and channel)

So i would like to add a part to the request where if there is no paddle number one, the number is added to the result set of the sub request.

Thanks for the replies !

I'd try something like that:

select pid from
(
    select 1 as pid
    union all
    select p.number+1 from Paddle
) as q
where
pid not in (select p.number from Paddle)

so, in sub-query q I'm getting all IDs which are probably empty and then filter out used ones

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM