How can I find in a column filled with numbers, the ones that are not used.
Table_A:
'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
'client_code' int(4) unsigned NOT NULL,
'client_name' varchar(50) NOT NULL,
PRIMARY KEY ('id')
INSERT INTO Table_A ('client_code','client_name') VALUES (1,'Bob');
INSERT INTO Table_A ('client_code','client_name') VALUES (2,'Anna');
INSERT INTO Table_A ('client_code','client_name') VALUES (5,'Jim');
INSERT INTO Table_A ('client_code','client_name') VALUES (6,'Tom');
INSERT INTO Table_A ('client_code','client_name') VALUES (10,'Mattew');
If I SELECT 'client_code' FROM Table_A
the output will be 1,2,5,6,10
. How can I have the numbers that are not used?
(3,4,7,8,9,11,12...)
Best Answer
To see what's missing, you can compare to a complete list.
There is no row generator in MySQL (like
generate_series()
in Postgres), but various surrogates are floating around. Like this one in the MySQL forums.Helper table to provide numbers from
0-9
:Building on that, generate a complete range of numbers:
Now you can generate the list of missing numbers, exactly as requested:
Key elements are the
LEFT JOIN
and the aggregate functionGROUP_CONCAT()
.To get full list of codes / names including missing codes, as may be your ultimate goal:
-> SQLfiddle demo.