Answer the question
In order to leave comments, you need to log in
How to count values from two MySQL tables?
Hello!
There is a request like this:
SELECT a.parent_id FROM users a WHERE a.user_id != 0 AND a.parent_id != 0 GROUP BY a.parent_id;
SELECT * FROM referals a WHERE a.parent_id = ИД_РЕФЕРЕРА
SELECT a.parent_id, COUNT(b.id) AS count_refs FROM users a, referals b WHERE a.parent_id = b.parent_id AND a.user_id != 0 AND a.parent_id != 0 GROUP BY a.parent_id
Answer the question
In order to leave comments, you need to log in
For example, like this:
SELECT distinct a.parent_id, b.cnt FROM users a
left join (
SELECT parent_id, count(id) cnt FROM referals group by parent_id
) b on a.parent_id = b.parent_id
WHERE a.user_id != 0 AND a.parent_id != 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question