简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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