当前位置: 首页 > news >正文

福州建设工程质量监督网站seo 360

福州建设工程质量监督网站,seo 360,互联网公司怎么找网站建设客户,计算机学院网站建设系统可行性分析大家好,我是空空star,本篇带大家了解一道中等的力扣sql练习题。 文章目录前言一、题目:608. 树节点二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果5.其他总结前言 …

大家好,我是空空star,本篇带大家了解一道中等的力扣sql练习题。

文章目录

  • 前言
  • 一、题目:608. 树节点
  • 二、解题
    • 1.正确示范①
      • 提交SQL
      • 运行结果
    • 2.正确示范②
      • 提交SQL
      • 运行结果
    • 3.正确示范③
      • 提交SQL
      • 运行结果
    • 4.正确示范④
      • 提交SQL
      • 运行结果
    • 5.其他
  • 总结


前言


一、题目:608. 树节点

给定一个表 tree,id 是树节点的编号, p_id 是它父节点的 id 。

+----+------+
| id | p_id |
+----+------+
| 1  | null |
| 2  | 1    |
| 3  | 1    |
| 4  | 2    |
| 5  | 2    |
+----+------+

树中每个节点属于以下三种类型之一:
叶子:如果这个节点没有任何孩子节点。
根:如果这个节点是整棵树的根,即没有父节点。
内部节点:如果这个节点既不是叶子节点也不是根节点。

写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。上面样例的结果为:

+----+------+
| id | Type |
+----+------+
| 1  | Root |
| 2  | Inner|
| 3  | Leaf |
| 4  | Leaf |
| 5  | Leaf |
+----+------+

解释
节点 ‘1’ 是根节点,因为它的父节点是 NULL ,同时它有孩子节点 ‘2’ 和 ‘3’ 。
节点 ‘2’ 是内部节点,因为它有父节点 ‘1’ ,也有孩子节点 ‘4’ 和 ‘5’ 。
节点 ‘3’, ‘4’ 和 ‘5’ 都是叶子节点,因为它们都有父节点同时没有孩子节点。
样例中树的形态如下:

注意
如果树中只有一个节点,你只需要输出它的根属性。

二、解题

1.正确示范①

提交SQL

select id,
case when p_id is null then 'Root' 
when id in (select p_id from tree) then 'Inner'
else 'Leaf' end Type
from tree
order by id;

运行结果

2.正确示范②

提交SQL

select id,
if(p_id is null,'Root',if(id in (select p_id from tree),'Inner','Leaf')) Type
from tree
order by id;

运行结果

3.正确示范③

提交SQL

select id,'Root' Type
from tree
where p_id is null
union all 
select id,'Inner' Type
from tree
where id in(select p_id from tree 
)
and  p_id is not null 
union all 
select id,'Leaf' Type
from tree
where id not in(select p_id from tree where p_id is not null 
)
and p_id is not null
order by id;

运行结果

4.正确示范④

提交SQL

select id,'Root' Type
from tree
where p_id is null
union 
select id,'Inner' Type
from tree
where id in(select p_id from tree 
)
and  p_id is not null 
union 
select id,'Leaf' Type
from tree
where id not in(select p_id from tree where p_id is not null 
)
and p_id is not null
order by id;

运行结果

5.其他


总结

正确示范①思路:
采用case when 语句
case when p_id is null then 'Root'
when id in (select p_id from tree) then 'Inner'
else 'Leaf' end Type
正确示范②思路:
采用 if 语句
if(p_id is null,'Root',if(id in (select p_id from tree),'Inner','Leaf')) Type
正确示范③思路:
采用union all 语句
正确示范④思路:
采用 union 语句

http://www.tj-hxxt.cn/news/61045.html

相关文章:

  • 北京网站建设哪家靠谱b2b免费发布信息平台
  • html5 网站建设新闻发布平台
  • 免费云电脑主机榜单优化
  • 龙岩长汀疫情最新消息今天天津网站优化软件
  • 福田网站(建设深圳信科)网站建设平台软件
  • 如何创建博客网站沈阳关键词seo
  • 做网站和推广找哪家好谷歌自然排名优化
  • 物联网平台功能批量优化网站软件
  • 崇信县门户网站百度保障客服电话
  • 建设网站主机可以用吗网店培训班
  • 网站价格seo排名优化排行
  • 影楼网站源码php创建网站要钱吗
  • 自己怎么做网站卖东西网页广告调词平台多少钱
  • icp许可证在哪个部门办理seo推广价格
  • 网站加入视频新手小白怎么学做运营
  • 男和男做那个视频网站好东莞做网站推广
  • 网站建设服务哪便宜免费培训机构
  • 佛山建设企业网站cpv广告联盟
  • 电子商务网站建设与维护案例推广运营
  • 网站做好了怎么做后台数字营销公司排行榜
  • 如何开 网站建设公司电商产品推广方案
  • 网站开发者模式网站百度收录突然消失了
  • .net如何做网站seo和竞价排名的区别
  • 北京网站建设 优化抖音seo查询工具
  • 哪个网站可以免费做国外网站如何给企业做网络推广
  • 阳江 网站建设百度如何推广网站
  • 社交网站开发项目计划报告搜索引擎的工作原理分为
  • 什么叫网页什么叫网站班级优化大师官网
  • 深圳网站建设智能 乐云践新seo公司北京
  • 建设一个网站的具体步骤成都seo排名