开发者

Return NULL columns if IDs don't exist in the table

开发者 https://www.devze.com 2023-01-30 03:06 出处:网络
I have one solution with left join for the below question, but I\'m looking for more efficient query Select * from table1 where Id in (1,2,3,4,5);

I have one solution with left join for the below question, but I'm looking for more efficient query

Select * from table1 where Id in (1,2,3,4,5);

returns all the existing "Id开发者_如何学Pythons" in the table. Now I want all the Ids to be returned with null columns if the Id is not existing in the table.

EX: Result must contain 3 and 5 though the IDs not existing in the table

ID  Name  Designation
1   John   Employee
2   Nar    Manager
3   **NULL**   **NULL**
4   Esh    Executive.
5   **NULL**   **NULL**


select x.id, y.name, y.designation
from (
select row_number() OVER(order by id) as id
from table1
) x
left join table1 y
on x.id = y.id

This ought to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消