sql - select code as ParentID if not null -


i have got table named users. there columns: id, code, parentid. parentid pointer id column, , can null. example:

id code  parentid 1 poland null 2 germany 1 

i following result. parentid code if not null , id code.

example(accordong above example):

parentid id null, poland germany, poland 

this self join... , poland parent of germany, not other way around according example. thus, expected results wrong.

declare @table table (id int, code varchar(64), parentid int) insert @table values (1,'poland',null), (2,'germany',1)  select     parentid = t2.code     ,id = t.code     @table t     left join     @table t2 on     t2.id = t.parentid 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -