题目太多就不写了,详情请看:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
题解:
涉及网址为:
CASE (Transact-SQL) - SQL Server | Microsoft Learn
SQL 中in 和not in 遇到NULL 值的查询情况_not in null_@北海怪兽的博客-CSDN博客
/* Write your T-SQL query statement below */
select
id,
--使用case when的搜索表达式即when后为true即可,还有一种是简单表达式即=即可。
case
when p_id is null then 'Root'
--这一步是关键,子查询中使用where是因为返回表中带有null,
--而使用not in是采用<>对比,不能对比。所以需要去除。
when id not in (select p_id from tree where p_id is not null) then 'Leaf'
else 'Inner'
end as 'type'
from
tree