简体   繁体   English

使用 SQL,如何从父子关系的两列中找到表中的子编号?

[英]Using SQL, how do you find the child number in a table from two columns where they are parent-child relationships?

在此处输入图像描述

From this table, after querying I want to get the child column that shows the number of the child for each id.从此表中,查询后我想获取显示每个 id 的子编号的子列。

I want:我想:

在此处输入图像描述

A scalar subquery will produce the data you want.标量子查询将产生您想要的数据。 For example:例如:

select
  t.*,
  (select count(*) from t b where b.parent_id = t.id) as child
from t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM