sql查询行替换为另一行

时间:2019-02-25 10:48:00

标签: sql

我在下面有一个查询

select id, name || ' ' || name as name, manager_id from table order by id;

返回

id name manager_id
 1 a             3
 2 b             3
 3 c             3

我要打印名称c代替manager_id为3。

2 个答案:

答案 0 :(得分:1)

您可以执行JOIN

SELECT t1.id, t1.name, t2.name AS Manger
FROM table1 t1 LEFT JOIN
     table2 t2
     ON t2.id = t1.manager_id
ORDER by t1.id;

如果该表位于同一表中,则可以执行self join

SELECT t.id, t.name, t1.name AS Manger
FROM table t LEFT JOIN
     table t1
     ON t1.id = t.manager_id;

答案 1 :(得分:0)

使用左连接并通过使用称为自连接的别名与相同的表进行两次连接

   select t1.id, t1.name , 
   t2.name as managername
   from table_name t1
   left join table_name t2
   on  t1.id=t2.manager_id