How to count and output the number of users recommended by each user at the same time when outputting the list of users?

the tuijian field is your own recommendation code in the table. Otuijian is the recommendation code that you fill in when you register.
when you output the user list, count and show the number of users" recommendations at the same time. I use the following method
$list =

     db("jianlis")
        ->alias("a")
        ->join("jianlis b","a.tuijian=b.otuijian")
        ->field(["a.*,count(b.id) as count"])
        ->group("a.name")
        ->paginate(15);
       

although user information and recommendation statistics can be found, because there are tens of thousands of items of data, it is too slow to query the data, is there a more convenient sql statement, or the model method of tp5?

Mar.22,2021

there is something wrong with building the table, can't you add one more referral field when you set up the table?
for example, rec_number INT (11) NOT NULL DEFAULT 0 COMMENT 'recommended number',

A recommends that B is registered. See the registration code of A when you register, and then find A-> setInt ('rec_number',1,5) according to A's registration code;

Menu