I would like to query some IDs (fixed values) into a table, like a "outer join" with the mandatory fields should be the fixed values. I have to do the same for SQLServer and MySQL, but with one solution I could adapt it to the another. Example:
Table X:
ID Name
1 A
2 B
3 C
4 D
Fixed Values:
1
3
10
In this case, I would like to get some like:
1 A
3 C
10 null
My query should be something like:
Select ID, Name
From X
Where X.ID IN (1,3,10)(+)
2
Answers
You could use a derived table and then a
UNION ALL
toSELECT
your arbitrary values instead a serviced table:Honestly, however, use the tools you have at your disposal for each RDBMS. For SQL Server, a
VALUES
table construct is much simpler:I’m not familiar with MySQL to comment if there’s a more succinct version than the
UNION ALL
method.Alternatively, you can use a subquery directly in the FROM clause: