SELECT u.id FROM user u WHERE u.id IN (7, 11)
if I have no results for the ids specified, I want to return all users, similar to this query :
SELECT u.id FROM user u
the difficulty and I want to keep the logical operator IN
to schematize my needs :
SELECT u.id FROM user u WHERE IF u.id IN (7, 11) = null THEN IN(*) ELSE IF u.id IN (7, 11) <> null THEN u.id IN (7, 11)
if possible avoid offering me a subquery, in the IN(*) part
2
Answers
In SQL Server you can actually use
TOP (1) WITH TIES
to do this very efficiently.But for MySQL you will need something like this
Another option is to use window functions
https://dbfiddle.uk/_yzWanfN