MySQL COUNT() with multiple joins - mysql

I am building a course system, with courses, instructors and a table to relate the two.
Symplified table instructors:
id | name | ...
Symplified table courses:
id | name | instructors_needed | ...
Symplified table link:
id | course_id | instructor_id
I created the following query to fetch the names of the instructors associated to a particular course:
SELECT i.name, c.name, c.instructors_needed FROM courses c
LEFT OUTER JOIN
link
ON c.id = link.course_id
LEFT OUTER JOIN
instructors i
ON link.instructor_id = i.id
This works fine. I created the following query to find the number of instructors on each course:
SELECT COUNT(i.name) as number, c.id, c.name, c.instructors_needed FROM courses c
LEFT OUTER JOIN
link
ON c.id = link.course_id
LEFT OUTER JOIN
instructors i
ON link.instructor_id = i.id
GROUP BY c.ID
I want to combine the two queries, to get all details about the instructors for a particular case, but also the total number of instructors on the course and the number of instructors needed. How do I do that? I understand that the GROUP BY is the problem here.
I searched but I could only find examples with 2 tables instead of 3, and I somehow can't figure it out for three tables.
Your help is really appreciated, thank you!

Try:
SELECT i.name, c.name, c.instructors_needed, Ctr.CourseCount FROM courses c
LEFT OUTER JOIN
link
ON c.id = link.course_id
LEFT OUTER JOIN
instructors i
ON link.instructor_id = i.id
LEFT OUTER JOIN
(SELECT link.course_id, COUNT(*) as CourseCount FROM link GROUP BY link.course_id) Ctr
ON link.course_id = Ctr.course_id

Related

Join 3 tables with MySQL

I have the following tables:
Products
prod_id | prod_name | prod_price
Supermarket
supermarket_id | name | address
supermarket_product
supermarket_id | product_id
How can I join those tables to get a table that shows all the information from Products and Supermarket tables in 1 single big table? I am basically trying to have a table only to hold which product belongs to which supermarket based on it.
I tried the following, but it doesn't work:
SELECT prod_name, prod_price, supermarkets.name
FROM products
INNER JOIN supermarkets
ON supermarket_product.supermarket_id = supermarket.supermarket_id;
You need to join products and supermarkets through bridge table supermarket_product:
select p.*, s.*
from products p
inner join supermarket_product sp on sp.product_id = p.prod_id
inner join supermarket s on s.supermarket_id = sp.supermarket_id
You need to also include supermarket_products in the query. You should also use table aliases to simplify your code:
SELECT p.prod_name, p.prod_price, s.name
FROM products p
INNER JOIN supermarket_product sp ON sp.product_id = p.prod_id
INNER JOIN supermarket s on s.supermarket_id = sp.supermarket_id

Return all courses a student is taking (Many to Many SQL Database example)

I'm fairly new to MySQL, and trying to understand the many-to-many relationship since these examples can popup in interviews
There are 3 tables, and since a Student can have many courses and a Course can have many students, this is a Many-to-Many relationship right?
The tables are
Student- has student ID, name, date of birth, and department.
Courses- Has ID, Name of course
Student_Courses- Has student_id, course_id
How would I display these 2 questions-
1) Given a studentID, return all the names of the courses the student is taking
2) Return the name of students who is taking X amount of courses or more (Ex. 4 or more courses).
Im trying to write queries on these, but I'm stuck...
In the case of selecting all of the courses for a given student ID you could try the following, which will return one row for each Course a Student is associated with.
select
s.name as StudentName,
c.name as CourseName
from `Student` as s
inner join `Student_Course` as sc on (sc.student_id = s.ID)
inner join `Course` as c on (c.ID = sc.course_id)
where
(s.`ID` = 'given_Student_ID_here')
;
As for selecting a list of the names of Students taking N or more courses, for this you might use an aggregating sub-select as a WHERE clause in which we reference one of the outer tables (i.e. [Student]) so that the result of the aggregation is personalised per Student record:
select
s.name as StudentName
from `Student` as s
where
(
(
select count(*)
from `Student_Course` as sc
inner join `Course` as c on (c.ID = sc.course_id)
where (sc.student_id = s.ID)
) >= 4
)
;
You might also consider an alternative approach to this second problem by using the GROUP BY and HAVING clauses:
select
s.name as StudentName
from `Student` as s
inner join `Student_Course` as sc on (sc.student_id = s.ID)
inner join `Course` as c on (c.ID = sc.course_id)
group by
s.name
having
count(*) >= 4
;

Querying multiple mySQL tables in a single query

I need to write a query for a scout database that compares the requirements of a badge with the skills a given member has already earned. The purpose being that several skills are applicable to multiple badges. My relevant tables (there are many) look like this:
Badge_Table:
Badge_ID,
Badge_Name,
Badge_Description,
Badge_Skills_Table:
Badge_Skill_ID,
Badge_ID,
Skill_ID,
Skills_Table:
Skill_ID,
Skill_Name,
Skill_Description,
Skills_Earned_Table:
Skills_Earned_ID
Skill_ID
User_ID
User_Table:
User_ID,
Name,
Age,
Address
Primary keys are shown in italics, and the foreign key relationships go from Badge_table to Badge_Skills_Table to Skills_Table to Skills_Earned_table to User_Table.
So far I have came up with the following ideas:
Selects all badges for named skill
SELECT badge_table.badge_name
FROM (badge_table
INNER JOIN badge_skills_table ON badge_ID
INNER JOIN Skills_Table ON skill_Id)
WHERE Skills_Table.Skill_Id = 1;
Selects all badges for each skill
SELECT badge_table.badge_name
FROM (badge_table
INNER JOIN badge_skills_table ON badge_ID
INNER JOIN Skills_Table ON skill_Id)
WHERE Skills_Table.Skill_Id = Skill_Badge_Table.Skill_Id
Selects all badges for named skill for named User - not quite working
SELECT badge_table.badge_name
FROM (badge_table
INNER JOIN badge_skills_table ON badge_ID
INNER JOIN Skills_Table ON skill_Id
INNER JOIN Skills_Earned_Table On skill_ID
INNER JOIN users_table ON user_ID)
WHERE Skills_Earned_Table.User_ID= 1 AND Skills_Earned_Table.SKILL_ID = Skill_Badge_Table.skill_ID
So can anyone help guide me with the following:
How to return all badges that a given skill is applicable for. (Done)
How to return all badges that a given scout has earned skills towards.
To return all badges the a given scout has earned all the skills for.
I'd appreciate any help you can offer,
You have no <conditions> in your ON clause. Try my query below:
SELECT A.badge_name
FROM badge_table A
INNER JOIN badge_skills_table B ON A.badge_ID=B.badge_ID
INNER JOIN Skills_Table C ON B.skill_Id=C.skill_ID
INNER JOIN Skills_Earned_Table D ON C.skill_ID=D.skill_ID
INNER JOIN users_table E ON user_ID ON D.user_ID=E.user_ID
WHERE D.User_ID= 1 AND D.skill_ID = B.skill_ID

get data from 3 relational tables

I have three tables Guardian, Student and StudentsGuardian. Table information is as under
Guardian:
id(pk)
Student:
id(pk)
name
address
StudentsGuardian:
student_id(fk)
guardian_id(fk)
I want to select those students whose guardian_id=2(suppose). Actually these are relational tables so i am unable to think a way to accomplish it. If i apply join it would return a joint table but i need only the information of those students having guardian_id= specific id.
It could be a basic question but i am stuck in it. Thanks
Use below query:
SELECT s.id, s.name, s.address
FROM Student s
INNER JOIN StudentsGuardian sg ON s.id = sg.student_id
WHERE sg.guardian_id = 'somespecific_id'
SELECT
*
FROM Guardian
INNER JOIN StudentsGuardian ON StudentsGuardian.guardian_id = Guardian.id
INNER JOIN Student ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
SELECT Student.name, Student.address
FROM Student JOIN StudentsGuardian ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
That should do.

Select data based on another table

I have three tables, I'll just list the important columns
db_players
id | name
players
id | teamid | careerid
db_teams
The db_teams id links to the players teamid.
I need to run a query where I select rows from db_players as long as db_teams.id isn't in a row in players as teamid where the careerid = 1.
I've never attempted this type of query with mysql before, I know I could do two queries and involve php but I'm intrigued as to whether it's possible with a pure db query.
Thanks.
EDIT - simpler now.
SELECT dp.first_name
FROM tbl_foot_career_db_players dp
INNER JOIN tbl_foot_career_players p
ON p.playerid != dp.id
WHERE p.careerid = 1
The idea is that I want to return all rows from tbl_foot_career_db_players WHERE the id from that table isn't present in a row in tbl_foot_career_players in the column playerid. And the tbl_foot_career_players.careerid must also equal 1.
List all db_players that are not in players with career = 1
SELECT d.*
FROM db_players d
LEFT JOIN players p
ON p.player_id = d.id
AND p.career = 1
WHERE p.id IS NULL
Try to JOIN them with db_players id is not in players teamid.
SELECT db_players.* FROM db_players
LEFT JOIN players ON players.id != db_players.id
LEFT JOIN db_teams ON players.teamid = db_teams.id
WHERE careerid = 1