I have two tables, users and teaching
The users table contains both teachers and students. The teaching table contains the relation between the two.
I want to be able to get the student based off who "teaches" them.
Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.
Any ideas as to how this is done? I would like it to return the id of the student.
I've tried doing a simple join statement, but this would fetch the teacher, not the student.
Thanks in advance
Something like that?
SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';
You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.
You can join the same table multiple times and that is totally legal in MySQL
Related
I'm trying to run a query that would SELECT * FROM teachers WHERE users.school = teachers.school AND WHERE users.grade = teachers.grade.
I'm setting up three tables, users, teachers and schools. The users are the parents and admins at the school, they sign up with an access_code that matches up in the school table. Same for the teachers, but their info is stored in the teacher's table. (Each user and teacher are associated with a school and grade both are in their tables). I want to show the user all the teachers info at the users' school.
I have all the info for the schools table id, school, and access_codes, but how would I go about achieving this? Would I use a JOIN or FOREIGN KEY?
I've tried some JOIN's, but I'm not that familiar with them. I looked at the FOREIGN KEY but not sure how to use it. Any help would be very much appreciated .
foreign key if for constarints
for build the relation you can use join eg:
SELECT *
FROM teachers
INNER JOIN users ON users.school = teachers.school
AND users.grade = teachers.grade
Anyway in you pseudo code you have only two table teachers and users
I kind a found a work around to my problem
SELECT * FROM teachers WHERE school =
(
SELECT school FROM users WHERE id = ".$id = $_SESSION['id']."
)
GROUP BY grade
This is a start it's not perfect but if I add another session for grade I think it will work.
I'm trying to find out which schools had students that did not complete their exams in 2018. So I've got 3 tables set up being: ExamInfo, ExamEntry and Students. I'm going to try use the ExamInfo table to get information from the Students table though, I obviously only want the student information that did not complete their exam in 2018. Note: I'm looking for students that attended, though did not complete the exam, with this particular exam you can look at completed exam as passed exam.
Within ExamInfo I have the columns:
ExamInfo_Date --when exam took place, using to get year() condition
ExamInfo_ExamNo --unique student exam ID used to connect with other tables
ExamInfo_Completed --1 if completed, 0 if not.
...
Within ExamEntry I have the related columns:
ExamEntry_ExamNo --connected to ExamInfo table
ExamEntry_StudentId --unique studentId used to connect to Students table
ExamEntry_Date -- this is same as ExamInfo_Date if any relevance.
...
Within Students I have following columns:
Students_Id --this is related to ExamEntry_StudentId, PRIMARY KEY
Students_School --this is the school of which I wish to be my output.
...
I want my output to simply be a list of all schools that had students that did not complete their exams in 2018. Though my issue is with getting from the ExamInfo table, to finding the schools of which students did not complete their exam.
So far I've:
SELECT a.Students_School, YEAR(l.ExamInfo_Date), l.ExamInfo_Completed
FROM ExamInfo l ??JOIN?? Students a
WHERE YEAR(l.ExamInfo_Date) = 2018
AND l.ExamInfo_Completed = 0
;
I'm not even sure if going through the ExamEntry table is necessary. I'm sure I'm meant to use a join, though unsure of how to appropriately use it. Also, with my 3 different SELECT columns, I only wish for Students_School column to be output:
Students_School
---------------
Applederry
Barnet Boys
...
Clearly, you need a JOIN -- two in fact. Your table has exams, students, and a junction/association table that represents the many-to-many relationship between these entities.
So, I would expect the FROM clause to look like:
FROM ExamInfo e JOIN
ExamEntry ee
ON ee.ExamEntry_ExamNo = e.ExamNo JOIN
Students s
ON ee.ExamEntry_StudentId = s.Students_Id
Ok So I am creating a Attendance System for a College and I am having trouble doing the tables in MySQL
The Problem I'm having is Grouping a group of students together for one class. So in MySQL
TABLE NAME: CourseGroup: courseGroup_id , student_id(FK), course_id(FK)
I want a number of students belonging to one class, this in hindsight will enable a teacher to undertake a register for that class group
In MySQL I have set the 'courseGroup_id as a unique field, however when I try to link 'courseGroup_id' in the Attendance table it only enables me to select 'student_id' and 'course_id'
Is it possible to select the courseGroup_id and display students for one class
My Attendance table looks like this:
Attendance: week_number, day, time, courseGroup_id, present
I want to be able to view all the students that belong to one class
I’m a bit lost with your terminology, you speak of “class id” but don’t reefer to that in your question – instead you use “course”.
I think the best option you can do here is create another table enabling a many to many relationship as many students can be registered to one or many courses. This type of table will enable a mapping between students and courses.
For example, if you create the mapping table “student_courses” with
student_id INT
course_id INT
You can then select students by course (joining the student table)
SELECT * from student_courses sc
INNER JOIN students s
ON s.student_id = sc.srudent_id
WHERE sc.course_id = ?CourseId
You can also reverse it and show the courses by for an individual course:
SELECT * from student_courses sc
INNER JOIN courses cs
ON cs.courset _id = sc.course_id
WHERE sc.student_id = ?StudentId
It is also worth pointing out that there should be two index on the student_courses, indexing both student_id and course_id for performance reasons.
I have two tables: teams, and persons.
table teams have three columns, id, name, leader
table persons have these columns: hash, team_id
teams.leader is a MD5 hash that must match persons.hash in order to determine which person is leader of a given team.
I need to run a query on MySQL that does the following:
1) Retrieve all the leaders of a team, and the team id:
SELECT `id`,`leader` FROM `teams`;
2) Use such information to update team_id on table persons
This is my current Query:
SELECT id FROM teams INNER JOIN persons ON teams.leader = persons.hash
but I haven't been able to come up with a solution that allows me update column team_id with the corresponding leader.
I've been thinking probably using cursors, but not sure about it.
Any ideas?
You can use the multiple table UPDATE syntax to join the tables:
UPDATE teams JOIN persons ON teams.leader = persons.hash
SET persons.team_id = teams.id
hi i have tables like this:
Persons:
person_id, name
then i have many langauge tables, that contain the languages people speak, the tables themselves only have the IDs. for example:
english:
person_id
then I also have a table that contains what schools they teach in, broken down to tables
for example:
havard:
person_id
To get those people that teach at havard and also speak english, I use the following query:
SELECT * FROM english LEFT JOIN havard.person_id = english.person_id
this will return the id of the person that can speak english and teaches at havard. How can I use that result to get the those people's name from the persons table? It's easy peasy with php, but I'm wondering if it's doable with mysql as well.
Here's a query that I believe answers your question:
SELECT person.name
FROM
english
JOIN harvard ON havard.person_id = english.person_id
JOIN persons ON persons.person_id = harvard.person_id
However, I would STRONGLY recommend against your current table structure. You shouldn't have many tables for languages, and many tables for schools. This will completely unmaintainable...
Instead, you should have a single language table, and a single school table. That way a new langauge or school being added to your table doesn't requrie schema or code changes.
To handle the many-to-many relationships, you could use a schema similar to the following:
Language
ID
Name
School
ID
Name
Language_Person
Language_ID
Person_ID
School_Person
School_ID
Person_ID
SELECT person.name
FROM
(english INNER JOIN harvard ON havard.person_id = english.person_id) INNER JOIN persons ON persons.person_id = harvard.person_id WHERE persons.person_id = "