当前位置: 首页 > 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.sczhlp.com/news/19136/

相关文章:

  • 8.18考试总结
  • 【题解】P13780 「o.OI R2」愿天堂没有分块
  • 网站两边广告软文什么意思
  • 江西网站建设哪家专业百度账号申请注册
  • 大理装饰公司做网站奶茶店营销软文
  • 小程序免费网站百度搜索量统计
  • Web3D项目需求陷阱 - 智慧园区
  • 宁德市路桥建设有限公司网站排名函数rank怎么用
  • 织梦网络公司网站源码ip域名解析查询
  • 绥德网站建设跨境电商营销推广
  • 最好的网站建设公司百度爱采购官网
  • 建个人博客网站武汉网站seo
  • 包装设计的意义seo网页优化服务
  • 建设银行app忘记登录密码百度seo公司整站优化
  • 网站里面的图片做桌面不清晰度怎么快速推广app
  • wordpress系统怎么样信息流优化师是做什么的
  • 合肥网站排名优化公司哪家好seo优化什么意思
  • QOJ #2214. Link Cut Digraph 题解
  • 字符串1
  • 王道408考研关于I/O部分的笔记
  • 多项式1
  • 怎么做阿里巴巴网站关键词挖掘ppt
  • 行业协会网站模板优帮云查询数据云查询
  • ps做专业网站惠州seo关键词推广
  • 大连网站建设工作室seo与网络推广的区别和联系
  • 国外独立网站建设企业网站定制开发
  • .net网站开发书百度联盟广告
  • 昆明网站建站平台交换链接营销的典型案例
  • 国内外政府网站建设借鉴seo推广费用
  • java开发手机网站营销策划方案公司