There are 2 tables
1) Contacts

id | Name 

2) Relatives

 id | Contact1 (контакт первый) | Contact2 (второй) | Family (кем приходится) 

There can be any number of connections per contact. It is necessary to derive contacts when outputting, if this contact has related relationships.

 SELECT contact.id, contact.name, COUNT(fam.id) FROM contact LEFT JOIN fam on contact.id = fam.contact or contact.id = fam.contact2 GROUP BY fam.id ORDER BY contact.name 

There is a problem: everything works, but in the base 743 contacts, and a maximum of 50 is output.

  • Looks like gorgeous) - KARTOH

2 answers 2

and if in GROUP BY add contact.id and contact.name, and fam.id to remove?

    And if so?

     SELECT contact.id, contact.name, COUNT(fam.id) FROM contact LEFT JOIN fam on contact.id = fam.contact1 or contact.id = fam.contact2 GROUP BY contact.id, contact.name ORDER BY contact.name