SQL Parent/Child recursive call or union?

后端 未结 5 1046
有刺的猬
有刺的猬 2021-02-10 11:04

I can\'t seem to find a relevant example out there.

I\'m trying to return a sub-set of a table, and for each row in that table, I want to check how many children it has,

5条回答
  •  猫巷女王i
    2021-02-10 11:40

    I believe this is what you are trying to do:

    SELECT P.PK_ID, P.Column1, P.Column2, COUNT(C.PK_ID)
    FROM
        Parent P
        LEFT JOIN Child C ON C.PK_ID = P.FK1
    GROUP BY
        P.PK_ID, P.Column1, P.Column2
    

提交回复
热议问题