Suppose that we have four tables in the following structure (Table name - fields):
person - id, name
doctor - id_person, specialty
pacient - id_person, disease
appointment - doctor_id, pacient_id, date
How can I construct a query to return the doctor's name and specialty, the pacient's name and disease and the appointment date?
Here's where I've got so far:
SELECT person.name, doctor.specialty, pacient.disease, appointment.date
FROM appointment
INNER JOIN person
ON appointment.pacient_id=person.id
INNER JOIN doctor
ON appointment.doctor_id=doctor.id_person
INNER JOIN pacient
ON appointment.pacient_id=pacient.id_person
But this is not returning the right fields. I think the problem resides in returning the same field (person.name) for two different ids (doctor and pacient) in the same row.
You need to do two separate joins to the person table, and use aliases to identify the individual tables like so:
select
dp.name as DoctorName
, doctor.specialty
, pp.name as PacientName
, pacient.disease
, appointment.date
from appointment
inner join doctor
on appointment.doctor_id = doctor.id_person
inner join person dp
on appointment.doctor_id = dp.id
inner join pacient
on appointment.pacient_id = pacient.id_person
inner join person pp
on appointment.pacient_id = pp.id
Related
1st table Courses columns:
courseId|instructorId|desc|courseName|img|
2nd table StudentCourses columns:
studentCourseId|studentId
3rd table UserData columns:
userId|avatar|name
So knowing studentId,i am trying to get students owned courses with its instructors profiledata(in this case avatar and name)
result columns should be:
courseId,courseName,desc,img,instructorsname,instructorsavatar
This query gives me students courseInfo but instructor data is not included.
SELECT Courses.courseId,Courses.img,Courses.courseName,Courses.`desc` FROM Courses JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"
assuming that instructorId match with userid
SELECT Courses.courseId
,Courses.img
,Courses.courseName
,Courses.`desc`
, u1.name
, u1.avatar
FROM Courses
INNER JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId
INNER JOIN UserData u1 ON u1.userId = Courses.instructorId
WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"
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
;
I am having a major problem joining 5 tables because each table only has 1 column in common with only 1 other table.
Here are my tables and columns in each table:
TABLE (COLUMNS)
person (person_id, first_name, last name)
building (building_id, building_name)
room (room_id, room_number, building_id, capacity)
meeting (meeting_id, room_id, meeting_start, meeting_end)
person_meeting (person_id, meeting_id)
OK, now here is what I am trying to do (pasted from a homework assignment):
Construct the SQL statement to find all the meetings that person_id #1 has to attend. Display the following columns:
Person’s first name
Person’s last name
Building name
Room number
Meeting start date and time
Meeting end date and time
Now I know how to join 2 tables but I have no idea how to pull info from 5 different tables like this.
I tried looking up how to do this and it just says to do a UNION command, and I am just learning and have yet to cover that.
As UNION is used to combine the result from multiple SELECT statements into a single result set, you don't need it for this scenario. You have to join all the tables one by one based on their Id.
SELECT P.First_Name, P.Last_Name, B.Building_name, R.Room_Number,
M.Meeting_Start, M.Meeting_End FROM Person P
JOIN Person_Meeting PM ON P.Person_Id = PM.Person_Id
JOIN Meeting M ON PM.Meeting_Id = M.Meeting_Id
JOIN Room R ON M.Room_Id = R.Room_Id
JOIN Building B ON R.Building_Id = B.Building_Id
WHERE P.Person_Id = 1
I have 3 tables one called Doctor, Person and one called Appointment. They have doctor_id in common relationship but only person holds their Last_Name.
I need to be able Produce a list of doctor IDs together with the number of appointments made for each doctor with zero or more appointment and also the doctors name.
How would I implement the the name of the doctor?
Here are the tables:
This is what I have so far:
SELECT doctor.doctor_id, COUNT(appt_time) AS No_APP
FROM doctor LEFT JOIN appointment ON doctor.doctor_id = appointment.doctor_id
GROUP BY doctor.doctor_id;
Just add one more left join for the new table :
SELECT doctor.doctor_id, COUNT(appt_time) AS No_APP, person.Last_name
FROM doctor LEFT JOIN appointment ON doctor.doctor_id = appointment.doctor_id
LEFT JOIN person on person.doctor_id = doctor.doctor_id
GROUP BY doctor.doctor_id;
Assuming the person table contains a link to doctors (will update when yo post third table if needed)
SELECT d.doctor_id, p.last_name, COUNT(a.appt_time) AS No_APP
FROM doctor d
LEFT JOIN appointment a ON d.doctor_id = a.doctor_id
LEFT JOIN person p ON d.doctor_id = p.doctor_id
GROUP BY d.doctor_id;
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