MySQL Forums
Forum List  »  General

Joining multiple tables
Posted by: Dash-Ulziit Oddelger
Date: March 02, 2020 04:02AM

Hello guys I am very bad at constructing SQL query. I have 3 tables having company detail and person in charge for that company and a license table. I am trying to list company information which will contain how many people in charge and license for that specific company.


if I join only one of the `license` or `person in charge` with

"SELECT `client_company`.`company_name`, COUNT(`client_contact`.`id`) AS `Contacts` FROM `client_company` JOIN `client_contact` ON `client_company`.`id` = `client_contact`.`company_id` GROUP BY `client_company`.`id`"

it is correctly retrieving the info BUT if I try to add one more JOIN it is multiplying or giving the numbers to only one row

"SELECT `client_company`.`company_name`, COUNT(`client_contact`.`id`) AS `Contacts`, COUNT(`license`.`id`) AS `Licenses` FROM `client_company` JOIN `client_contact` ON `client_company`.`id` = `client_contact`.`company_id` JOIN `license` ON `license`.`company_id` = `client_company`.`id` GROUP BY `client_company`.`id`"

How can I retrieve this kind of information on a single query?
Thank you.

Options: ReplyQuote


Subject
Written By
Posted
Joining multiple tables
March 02, 2020 04:02AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.