Query:
select id from users where id in (1,2,3,4,5)
If the users table contains ids 1, 2, 3, this would return 1, 2, and 3. I want a query
select missing.id
from
(select ELT(@indx, 1,2,3,4,5) as id, @indx:=@indx+1
from (select @indx:=1) init,
users
where ELT(@indx, 1,2,3,4,5) is not null
) missing
left join users u using(id)
where u.id is null;
What you have in here is:
ELT
together with the variable @indx
allows you to 'transpose' the list into a column. (select @indx:=1)
is needed to initialize indx to 1 users
table in the inner select is needed so that MySQL has something to iterate on (so the size of your list cannot exceed the number of the rows in users table, if that's the case that you can use any other table that is big enough instead of inner users, again, table itself does not matter it's just to have something to iterate on, so only its size that matters).ELT(@indx, 1,2,3,4,5) is not null
condition in the nested select is to stop iteration once you are at the index exceeding your list size.The rest is simple - left join
and check for null
.