sql自关联查询上级id对应的名字

s

1,表结构:city_info

  

 

 

 2,实体类

  

 

 

 3,sql:

  方法一:关联查询

  select ci1.id,(select ci2.name from city_info ci2 where ci2.id = ci1.pId) as pName,ci1.name,ci1.title,ci1.type from city_info ci1;

  方法二:左外连接查询

  select ci1.id,ci2.name pName,ci1.name,ci1.title,ci1.type from city_info ci1 LEFT JOIN city_info ci2 ON ci1.pId = ci2.id;

4,查询结果

  

posted @ 2019-12-12 11:24  向往黎明小豆子  阅读(1844)  评论(1编辑  收藏  举报