Right now i have these two tables
doctor_id and their appointment count
and doctor name and his/her department name
I need to count every department appointments any idea how to connect these two tables ?
Just join all the tables and change the grouping.
SELECT d.name AS department, COUNT(r.d_id) AS appointment_count
FROM departments AS d
JOIN userdelprel AS ud ON d.dept_id = ud.dept_id
JOIN users AS u ON u.u_id = ud.u_id
LEFT JOIN roomreservations AS r ON r.d_id = u.u_id
WHERE u.role_id = 2
GROUP BY d.dept_id
Related
I need the count of doctors, residents, nurses, and patients per department. However, it yields no results. If I remove 3 types, it gives me a list of counts of remaining person type. How to make the results appear when having all person types included?
Thanks.
`SELECT DISTINCT
department.departmentID,
COUNT(doctor.doctorID) AS number_of_doctors,
COUNT(resident.residentID) AS number_of_residents,
COUNT(nurse.nurseID) AS number_of_nurses,
COUNT(patient.patientID) AS patient_count
FROM department
INNER JOIN person ON department.departmentID = person.departmentID
INNER JOIN doctor ON doctor.employeeID = person.personID
INNER JOIN resident ON resident.employeeID = person.personID
INNER JOIN nurse ON nurse.employeeID = person.personID
INNER JOIN patient ON patient.patientID = person.personID
GROUP BY department.departmentID'
Presumably, you can't have a person that is simultaneously doctor, resident, nurse and patient. So use left joins :
SELECT
de.departmentID,
COUNT(do.doctorID ) AS number_of_doctors,
COUNT(re.residentID) AS number_of_residents,
COUNT(nu.nurseID ) AS number_of_nurses,
COUNT(pa.patientID ) AS patient_count
FROM department de
INNER JOIN person pe ON de.departmentID = pe.departmentID
LEFT JOIN doctor do ON do.employeeID = pe.personID
LEFT JOIN resident re ON re.employeeID = pe.personID
LEFT JOIN nurse nu ON nu.employeeID = pe.personID
LEFT JOIN patient pa ON pa.patientID = pe.personID
GROUP BY de.departmentID
I have a one to many relationship between departments and users.
Database Design:
users has each own department and department has many users. I wanted to select all the department_name but I have a duplicate values of department_name I want to merge it into one so I planned to use UNION how can I implement this using Inner Join? This is my code so far.
SQL
SELECT D.department_name FROM users U
INNER JOIN departments D ON D.id = U.department_id;
Results:
If you want only the distinct department names, you need to group the users into a comma separated values.
select d.department_name, group_concat(u.id) user_id_list
from departments d inner join users u on d.department_id = u.department_id
group by d.department_name
I have a bills table with column customer_type and customer_id fields.
This customer_type tells if the customer is in the customers table or in the users table or in the suppliers table.
I need to create a query with left join according to customer_type.
select c.* from bills b
left join ***b.customer_type*** c on c.id = b.customer_id
You could join all three with necessary condition:
select c.*, u.*, s.* from bills b
left join customers c on c.id = b.customer_id and b.customer_type = 'customers'
left join users u on u.id = b.customer_id and b.customer_type = 'users'
left join suppliers s on s.id = b.customer_id and b.customer_type = 'suppliers'
Then you can take the data that is relevant from the result.
However if there are similar columns in these 3 tables you might want to restructure the database to only store one type of information in one place.
I have a table with persons (personid,name), another table with camps (campid,title) and a third table with camp participations (id,personid,campid).
Now, for a given camp, I need a list of all other camp participations by all persons participating in the current camp.
But I have no idea how to join these tables. I have looked at a lot of other examples, but I can't really seem to get any inspiration from those...
This should work:
SELECT *
FROM PERSONS AS D INNER JOIN CAMP_PARTICIPATIONS AS E ON D.PERSONID = E.PERSONID
INNER JOIN CAMPS AS F ON F.CAMPID = E.CAMPID
WHERE F.CAMPID <> [your_camp] AND A.PERSONID IN (
SELECT A.PERSONID
FROM PERSONS AS A INNER JOIN CAMP_PARTICIPATIONS AS B ON A.PERSONID = B.PERSONID
INNER JOIN CAMPS AS C ON C.CAMPID = B.CAMPID
WHERE C.CAMPID = [your_camp] )
select persons.*,participations.*,camps.* from persons left join participations
on participations.personid=persons.personid
left join camps on camps.campid=participations.campid
where camp.campid=1;
now u can change the campid in where clause and place column name which u want in select clause
I have a users table which contains the users information (fname, lname...etc) and a invoices table. In the invoices table I have a field called created_id which links to the user.id that created the invoice. I also have a field called staff_id which links to the staff user.id that approved the invoice.
How can I query the first and last name for both the created_id and the staff_id in a single query? Here are a few things I've tried....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname
FROM
invoices
INNER JOIN users
ON users.id = invoices.created_id;
This works, but it only gets me the person's name that created the invoice. How can I add the staff's name to that as well....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname,
users2.fname as staff_fname,
users2.lname as staff_lname
FROM invoices, users
LEFT JOIN
invoices,
users AS users2
ON
users.id = invoices.created_id,
users.id = users2.id
That doesn't work, but is closer. Any guidance or examples would be very helpful. Also, if you have any recommendations for good books on learning how to do more advanced MySQL queries that would be helpful too.
You need to join users table twice on table Invoice.
SELECT a.*,
b.fname created_firstName,
b.lname created_LastName,
c.fname staff_firstName,
c.lname staff_LastName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id
and best thing is you can concatenate their names into one using CONCAT
SELECT a.*,
CONCAT(b.fname, ' ', b.lname) created_fullName,
CONCAT(c.fname, ' ', c.lname) staff_fullName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id