Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner join with 3 tables in mysql

I want to select data from more tables with Inner join.

These are my tables.

Student (studentId, firstName, lastname) Exam (examId, name, date) Grade (gradeId, fk_studentId, fk_examId, grade) 

I want to write a statement that shows which exam, grade and date alle the students have been to. Sorted after date.

This is my statement. It runs, but i want to make sure that i am doing it correctly.

SELECT   student.firstname,   student.lastname,   exam.name,   exam.date,   grade.grade FROM grade   INNER JOIN student     ON student.studentId = grade.gradeId   INNER JOIN exam     ON exam.examId = grade.gradeId ORDER BY exam.date 
like image 621
Zincktest Avatar asked Apr 15 '13 10:04

Zincktest


People also ask

Can you inner join 3 tables mysql?

It is possible to use multiple join statements together to join more than one table at the same time. To do that you add a second INNER JOIN statement and a second ON statement to indicate the third table and the second relationship.

How join 3 tables inner join SQL?

The syntax for multiple joins: SELECT column_name1,column_name2,.. FROM table_name1 INNER JOIN table_name2 ON condition_1 INNER JOIN table_name3 ON condition_2 INNER JOIN table_name4 ON condition_3 . . . Note: While selecting only particular columns use table_name.

Can you cross join 3 tables in SQL?

SQL Server CROSS JOIN Tutorial The CROSS JOIN is used to show every possible combination between two or more sets of data. You can do a cross join with more than 2 sets of data.


1 Answers

Almost correctly.. Look at the joins, you are referring the wrong fields

SELECT student.firstname,        student.lastname,        exam.name,        exam.date,        grade.grade   FROM grade  INNER JOIN student ON student.studentId = grade.fk_studentId  INNER JOIN exam ON exam.examId = grade.fk_examId  ORDER BY exam.date 
like image 81
agim Avatar answered Sep 23 '22 13:09

agim