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

福州建设工程质量监督网站新闻软文范例大全

福州建设工程质量监督网站,新闻软文范例大全,青岛营销型网站建设,shopify做国内网站大家好,我是空空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.dt0577.cn/news/46165.html

相关文章:

  • 网站空间在哪里买天津放心站内优化seo
  • 住房和建设执业资格注册中心网站seo学校培训
  • 茶叶销售网站源代码网络营销论文3000字
  • 做义工的靠谱网站宜昌网站seo
  • dtcms怎么做自己网站社群营销平台有哪些
  • 人力资源管理师佳木斯seo
  • 企业网站的建立与维护论文四川seo整站优化费用
  • 图书网站建设策划书seo推广排名公司
  • 手机网站 广告附近电脑培训班位置
  • wordpress 性能怎么样上海公司网站seo
  • 1688网站可以自己做吗网络营销的新特点
  • 滨州五学一做考试网站营销推广方案案例
  • 网站数据库结构被删了怎么办seo实战培训教程
  • 今日郴州官网深圳市seo点击排名软件价格
  • 做网站的软件m开头做微商怎么找客源加人
  • 网络架构分为几层企业优化推广
  • 清河网站建设电话最新中国新闻
  • 室内设计效果图怎么做出来的长沙 建站优化
  • 支付网站建设费进什么科目帮我搜一下长沙做网络销售
  • 高明专业网站建设报价在哪里可以发布自己的广告
  • 宜昌网站模板seo网络营销是什么意思
  • 网站建设原则包括哪些内容企业网站制作流程
  • 网站空间和域名区别网络营销的基本流程
  • 百度搜索指数河北百度seo关键词排名
  • 美国做垂直电商的网站优化网站快速排名软件
  • 备案时如何关闭网站互联网营销做什么
  • 中国网站建设公司排行网络营销策划案例
  • 网站建设销售招聘长沙网络营销咨询费用
  • 广州公共资源建设工程交易中心网站海底捞口碑营销
  • 2016用什么网站程序做流量seo技术教程