Hello, I have the following problem: I made a micro-social network for managers, where initially the registration is done only by invitation, thus "measuring" the influence of each one. My problem is in the ranking that currently counts the invitations accepted by each person and the one with the most accepted invitations, obviously it is at the top of the ranking:
SELECT
u.usr_id, b.ref_id, u.nome, b.c_hab, COUNT(*) AS qnt
FROM
tbl_user AS u
INNER JOIN
tbl_user b ON u.usr_id = b.ref_id
WHERE
b.c_hab = 1
GROUP BY u.nome
ORDER BY qnt DESC;
My problem arose when they asked me to count the direct invitations, that is, the manager sent to so-and-so (we will call it level 1), the invitations that so-and-so sent (level 2) and so on. It's confusing to explain, but basically a Multilevel Marketing system and count the invitations invitations, but I have no idea how to accomplish this ...
Follow the users table for further details:
CREATE TABLE 'tbl_user' (
'usr_id' smallint(6) NOT NULL,
#Aqui é onde são armazenados as IDs de quem convidou você
'ref_id' smallint(6) DEFAULT NULL,
'nome' varchar(100) COLLATE utf8_unicode_ci NOT NULL,
'email' varchar(50) COLLATE utf8_unicode_ci NOT NULL,
'dt_nascimento' date DEFAULT NULL,
'cel' varchar(15) COLLATE utf8_unicode_ci NOT NULL,
'endereco' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
'numero' varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
'senha' varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
'foto' varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
'token' char(10) COLLATE utf8_unicode_ci DEFAULT NULL,
'c_hab' char(1) COLLATE utf8_unicode_ci DEFAULT '0',
'nivel' char(1) COLLATE utf8_unicode_ci DEFAULT '1',
'candidato' varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
'dt_cad' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
If you can give a possible solution, thank you in advance!