Answer the question
In order to leave comments, you need to log in
How to get a list of numbers missing in a PostgreSQL database?
The database contains the following data (for example):
Answer the question
In order to leave comments, you need to log in
Need a reference list of values and a not exists subquery. The reference list can be made via the generate_series function
select n from generate_series(1,11) as n
where not exists (select from tablename where id = n)
IMHO, if such a question arose, then it is POSSIBLE that you are doing something wrong. Broadcasting business sense on IDb, the main purpose of which is to ensure uniqueness, is generally not very correct.
Nevertheless...
Perhaps there is a more beautiful solution, but it does not immediately occur to me (it will be different for each database).
General approach:
1. Create a table t2 with a large number of rows and one field num. In the loop, fill it with values from 1 to n.
2. In the query, minus the source table from t2
SELECT t.num FROM t2
EXCEPT
SELECT t.id FROM base_table t
all.
PS If you are too lazy to make a table, and the number of records is small, then you can use
SELECT unnest (array [1, 2, 3, 4)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question