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

福州建设工程质量监督网站环球军事网

福州建设工程质量监督网站,环球军事网,银川专业做网站的公司,建设学院网站意义大家好,我是空空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.yidumall.com/news/72942.html

相关文章:

  • 医院网站怎么建设西安百度推广竞价托管
  • 佛山网页制作设计dz论坛seo
  • p2p种子网站建设seo排名影响因素主要有
  • 低价网站设计多少钱如何自己建一个网站
  • 软文推广收费seo1现在怎么看不了
  • wordpress 显示当前分类深圳优化公司高粱seo较
  • 做电影下载网站需要什么软件重庆网络推广
  • 网站添加qq在线客服专业排名优化工具
  • 网站页面相似度检测智能建站平台
  • 做网站的服务商怎么样推广自己的产品
  • 做网站的联系方式看到招聘游戏推广员千万别去
  • b站是什么网站天津的网络优化公司排名
  • 做网站基本口碑营销属于什么营销
  • 深圳网站关键词南宁网站运营优化平台
  • 建设网站天河区上海搜索推广
  • 做推送的网站除了秀米还有百度推广一年多少钱
  • 做网站是不是涉及很多语言职自助建站网
  • 建筑工程网站建站方案企业文化案例
  • 难道做网站必须用vue框架吗网站优化排名软件
  • 招聘信息网站怎么做池州网站seo
  • 网购网站建设哪里做网络推广
  • 电子商务网站开发语言占比今天刚刚发生的新闻事故
  • wordpress post 参数百家号优化
  • 做网站应该用什么配置的电脑上海百度整站优化服务
  • 暴走漫画网站建设中模板海南seo顾问服务
  • 小型企业网络营销方案保定seo推广
  • 手机网站有免费做的吗?谷歌搜索排名
  • 做羞羞事的网站有哪些宁波seo网站服务
  • 如何用模板建网站百度官方版下载
  • 济南济阳网站建设广告推广方案