COUNT via multi-chain join - mysql

I have this hierarchy in my database (from lowest to highest):
User => Dept => Area => Company
Now I need to make a table that shows all companies (some info about them taken directly from companies table) but the last column in the HTML table I want to be Number of users. I know I need to join the tables together and perhaps join table to itself, but how do I do this?
Each of these tables have a column linking to its parent table (except Company ofc).

JOIN the tables:
SELECT
c.companyId,
c.CompanyName,
IFNULL(COUNT(u.userID), 0) AS 'Number Of Users'
FROM Company AS c
LEFT JOIN Area AS a ON c.CompanyID = a.CompanyID
LEFT JOIN Dept AS d ON a.DeptId = d.DeptId
LEFT JOIN users AS u ON D.UserId = u.UserId
GROUP BY c.companyId,
c.CompanyName;
Note that: LEFT JOIN with IFNULL will give you those companies that has no matched rows in the other tables; with count zero in this case

Related

How do you do multiple joins three joined tables to a fourth table in MYSQL

The database schema for this question is located here: db fiddle
I am trying to do the following:
Join table A (group table) to table J (grouprooms)
Join table B (room table) to table J (grouprooms)
Finally Join table C (users) table A - note table A already joined to J
I have written a query that accomplishes steps 1 and 2 but can't figure out how to join the users table to the group table.
Here's the query I have so far:
select rooms.room_name, groups.group_name, groups.group_ID
from grouprooms
left join rooms on grouprooms.room_ID = rooms.room_ID
left join groups on grouprooms.group_ID = groups.group_ID;
You could just add another LEFT JOIN to your query to link in table users :
select rooms.room_name, groups.group_name, users.name
from grouprooms
left join rooms on grouprooms.room_ID = rooms.room_ID
left join groups on grouprooms.group_ID = groups.group_ID
left join users on grouprooms.group_id = users.group_id
See your updated db fiddle :
grouproom 6 is showing no user because there is no user in group 3 (OPERATIONS)
grouproom 7 is not linked to a group, hence it has no group name and no user

sql count(*) query on results of another query performing multiple results

how to perform sql count(*) query on results of another query performing multiple results ?
I've 3 tables :--
user_subscribe table where I've 'user_id' and other columns.
user table where we have 'user_id' & 'country_id' columns where user_id is being linked with user_subscribe table and country_id is being linked with 'country' table.
country table having 'country_id' column.
I need to get total count of user_subscribe based on users (user_id) who belongs to same country.
A Help is highly appreciated as I'm being stuck on this problem from last 7 days.
SELECT COUNT(US.user_id) as country_user_subscribed
FROM user_subscribe as US
LEFT JOIN users as U ON user.id=US.user_id
LEFT JOIN countries as C on C.id=U.country_id
GROUP BY C.country_id
Try This Solution :
SELECT C.CountryName,Count(u.UserId) AS UserCount FROM User_Subscribe AS us
LEFT JOIN User AS u ON us.UserId = u.UserId
LEFT JOIN Country AS c ON u.CountryId = c.CountryId
WHERE c.CountryId = 3
GROUP BY c.CountryName

Joining three tables in SQL, Inner join does not work properly

We have three tables, document,department and contact.
All the tables are linked by an 'id' column. I want the result as follows
firstname lastname address upload_date department_name
The below query fetches the first four columns
SELECT contact.firstname, contact.lastname,contact.address ,
document.upload_date
FROM contact
JOIN document
ON document.id= contact.id
AND contact.status = 1
AND document.defaultdoc=1
So it's an inner join.
But to fetch the last column, the department_name I added a similar join with contact.deptId=department.id, but the query returns zero results. Anything wrong ?
if you add
JOIN department
ON contact.deptId=department.id
it should work.
If all deptId's exist in its master table department then you can use below query:
SELECT c.firstname, c.lastname,c.address ,
doc.upload_date, d.department_name
FROM contact as c
JOIN document as doc
ON doc.id= c.id
AND c.status = 1
AND doc.defaultdoc=1
JOIN department as d
on c.deptId=d.id;
But if deptid updated in contact table does not exist in department table (which should not be if you want referential integrity in your db), then you can use below query:
SELECT c.firstname, c.lastname,c.address ,
doc.upload_date, d.department_name
FROM contact as c
JOIN document as doc
ON doc.id= c.id
AND c.status = 1
AND doc.defaultdoc=1
LEFT JOIN department as d
on c.deptId=d.id;
If both don't work then show your table data and structure.

mySQL- how to show all contact and their status by using INNER JOIN

This is my company Table
CompanyID, CompanyName
This is my Contact Table
ContactID, ContactName, CompanyID
This is my Report Table
ReportID, ReportName
This is my ReportContact Table, Many to Many Relationship
ContactID, ReportID
I want to return all ALL my CONTACTID of 1 company, include those who are not assign to any report, I also want to return the reportID that are assign to different contacts
1 contacts can be assign to many reports
1 reports can consist of many contacts
My current SQL CODE only manage to get the 2 contactID in the ReportContactTable
SELECT rc.ContactID, rc.ReportID from contact c INNER JOIN Reportcontact rc on c.ContactID = rc.ContactID Where CompanyID=1
how can Return all the contact include those not in the reportcontact table, but get the reportID at the same times?
INNER JOIN filters out those rows that are not in ReportContact. Try to use LEFT JOIN if you want all contacts from contact table.
SELECT rc.ContactID, rc.ReportID
FROM contact c LEFT JOIN Reportcontact rc
ON c.ContactID = rc.ContactID
WHERE CompanyID = 1
I'm not 100% sure I understand what you are trying to do, but if you want all rows from the contact table then you need to use an OUTER rather than INNER join.
Try changing the word INNER to LEFT.
An INNER join ensures that rows that satisfy the join condition appear in both tables.
An OUTER join says "show me all the rows in one table, plus those that satisfy the join condition from the other table". Which table shows all the rows depends on the use of the keywords LEFT and right
a left join b on a.id = b.id will show all rows from table a plus those from b which satisfy the join condition
a right join b on a.id = b.id will show all rows from table b plus those from a which satisfy the join condition

Hybrid Left/Right Join based on condition?

I'm trying to write an SQL statement to retrieve a list of users from a database, along side their company name (if they have a company associated with them). However, there are a couple gotchas:
Not all users have companies, but I still need to show these people in the list.
Even if a user has a company, that company could be soft-deleted (the record is still in the database, but is flagged with is_deleted = 1), and I don't want to show users that are associated with "deleted" companies.
So essentially I want to SELECT from the User table and LEFT JOIN the company table, but I don't want to include the User record at all if the company they are assigned to is_deleted.
My first inclination is that I would have to use a UNION to merge two queries together, but I was hoping there would be a cleaner way to do it?
Using Mysql 5.1
SELECT U.name Username, C.name Company
FROM User U
LEFT OUTER JOIN Company C
ON U.companyid = C.id
WHERE C.id IS NULL OR C.is_deleted = 0
C.id IS NULL gets the users with no company, and C.is_deleted = 0 gets the users with companies that haven't been soft-deleted.
Try joining to a table that excludes the deleted companies:
SELECT U.Name, C.Name
FROM User U LEFT OUTER JOIN
(SELECT CompanyId, CompanyName
FROM Company
WHERE is_deleted = 0)
C ON U.CompanyId = C.CompanyId