get data from 3 relational tables - mysql

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.

Related

my sql output the same data more than one time when i use select statement

I created three tables "student" and "course" and "lecturer" and I inserted data into them.
Now I want to retrieve some data by select.
When I want to show: Subject taken by Kumar
SELECT STUDENT.NAME, COURSE.SUBJECT1, COURSE.SUBJECT2, COURSE.SUBJECT3
FROM STUDENT,COURSE
WHERE STUDENT.COURSE = COURSE.COURSE = 'MLVK'
it repeats the data more than one time.
I hope anyone help me
All the best
Learn to use proper, explicit, standard JOIN syntax.
And, table aliases:
SELECT s.NAME, c.SUBJECT1, c.SUBJECT2, c.SUBJECT3
FROM STUDENT s JOIN
COURSE c
ON s.COURSE = c.COURSE
WHERE c.COURSE = 'MLVK'
If I were going to use these tables to pick subjects taken by Kumar, I would write something like:
SELECT
s.name, c.course, c.subject1, c.subject2, c.subject3
FROM
student as s
LEFT JOIN course as c on c.course = s.course
WHERE
s.no_matrik = '23456'
GROUP BY
s.name, c.course, c.subject1, c.subject2, c.subject3
I think this version makes the intent slightly more clear (pick subject for a particular student, Kumar) than the previous answer (select subjects for any student having course = 'MLVK'). This answer will also return information for Kumar even if he has no course value in the Student table (pre-enrollment?).
First of all I would suggest that to model your data properly in BCNF form, where you should have modelled another table to persist subjects and map the lecturer who take that subject.
Ex: (Subject Table)
SubjectId LECT_ID
---------- ------------
TT234 L123
TT235 L003
and, your Course table would be more of course to subject mapping, like:
CourseName SubjectId
------------- --------------
DTM TT235
DTM TT695
...
then you use query as:
Select sub.SubjectId, l.NAME
From
Student s JOIN
Course c
on c.CourseName = s.COURSE
JOIN Subject sub
on sub.SubjectId = c.SubjectId
JOIN Lecturer l
on l.LECT_ID = sub.Lecturer
Where s.NAME = 'Aminah'
the above query will result as:
SubjectId NAME
--------- ----------
PP563 Ahmad
SS003 Ahmad
PP999 John
as Ahmad happens to be teaching 2 subjects in course DPG. but if you wish to report distinct Lecturers for Aminah, you can change query as:
SELECT NAME FROM Lecturer where LECT_ID
in (
Select l.LET_ID
From
Student s JOIN
Course c
on c.CourseName = s.COURSE
JOIN Subject sub
on sub.SubjectId = c.SubjectId
JOIN Lecturer l
on l.LECT_ID = sub.LECT_ID
Where s.NAME = 'Aminah'
) a

Get all data from a table that contains two foreign key data

Table instructor: ID, name, dept_name, salary
Table student: ID, name, dept_name, tot_cred
Table advisor: s_ID, i_ID which contains student id and instructor id for the two table.
I need to find all the instructor and student's name where the department of the advisor is CComp.Sci
I can find all the id of the instructor and student whrer the intructor is fom Computer science. And only the name of the students.
But can't figure out both the name at the same time.
I wrote this:
SELECT student.name
FROM student
WHERE student.ID in (SELECT advisor.s_ID
FROM advisor
, instructor
WHERE advisor.i_ID = instructor.ID
and instructor.dept_name = 'Comp.Sci')
The root solution I think you need here is just a simple join between the three tables. But since you need a single list of both student and instructor names, this complicates things. One option is to union together a query which finds the matching students along with a query which finds the matching instructors.
SELECT s.name, 'student' AS type
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
UNION ALL
SELECT DISTINCT i.name, 'instructor'
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
Using "where .. in (..)" will give you bad performance, also it will not allow you to get the data from the table in the where clause.
Here is a solution if you mean to get the result of instructor name side alone with student name.
SELECT S.name AS Student,I.name AS Instructor
FROM Students AS S
JOIN Advisor AS A ON A.s_ID = S.Id
JOIN Instructor AS I ON I.Id = A.i_ID
WHERE I.dept_name = 'Comp.Sci'
*notice that I used alias
** if you want for example to get all students even those who lack instructor use LEFT JOIN

I want to view many fields from 3 table to show them in same page and same query

Hi
I have this three table . and i have form to put the id_student then the view must be :
the name + surname +father then
the subjects and the mark of each subjects
how i can do that ?
i try this :
select students.name, grade.marks
from students
INNER JOIN grade ON students.id_students = grade.id_students
You are almost there, you need to join with the third table as well. Considering you want all the column from 3 table do something like
select students.*,
grade.*,
Courses.*
from grade
INNER JOIN courses on grade.id_subject = courses.id_subject
INNER JOIN students ON students.id_students = grade.id_students
WHERE students.id_students=690

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

MySQL COUNT() with multiple joins

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