SOLVED: I forgot to select for a certain value so the query was just returning the first grade it found.
I have 2 tables and I would like to combine part of one and part of the other
The first table, Grades, has
COURSE_NUMBER
, SECTION_NUMBER
, GRADE_A
, GRADE_B
GRADE_C
The second table, Sections, has
COURSE_NUMBER
, SECTION_NUMBER
, INSTRUCTOR
I would like to output the results so I can see
COURSE NUMBER | SECTION NUMBER | INSTRUCTOR | GRADE A | GRADE B | GRADE C
I've tried using JOIN and i'm getting the correct course and section number with the correct instructor. However the grades are not matching up with the course and section number from the first table.
Heres the query that gets me the incorrect grades
SELECT Grades.COURSE_NUMBER, Grades.SECTION_NUMBER, Sections.INSTRUCTOR,
Grades.A, Grades.B, Grades.C
FROM `Grades` INNER JOIN `Sections` on Grades.COURSE_NUMBER = Sections.COURSE
and Grades.SECTION_NUMBER = Sections.SECTION
I don't have a lot of experience with databases so any suggestions on simplifying this problem are also appreciated.
Thank you
EDIT:
Sample data for the Grades table:
| Course_number | Section_number | A | B | C |
| 101 | 001 | 4 | 1 | 0 |
| 101 | 002 | 5 | 2 | 8 |
| 102 | 001 | 7 | 9 | 4 |
Sample data for the Section table:
| Course_number | Section_number | Instructor |
| 101 | 001 | Alex |
| 101 | 002 | Jeff |
| 102 | 001 | Greg |
Expected Result:
| COURSE NUMBER | SECTION NUMBER | INSTRUCTOR | GRADE A | GRADE B | GRADE C |
| 101 | 001 | Alex | 4 | 1 | 0 |
| 101 | 002 | Jeff | 5 | 2 | 8 |
| 102 | 001 | Greg | 7 | 9 | 4 |
Not-working Result:
| COURSE NUMBER | SECTION NUMBER | INSTRUCTOR | GRADE A | GRADE B | GRADE C |
| 101 | 001 | Alex | 25 | 9 | 2 |
| 101 | 002 | Jeff | 0 | 34 | 6 |
| 102 | 001 | Greg | 2 | 3 | 12 |
(The grades are just being randomly pulled from somewhere in the Grades table, its very large)
Your query seems conceptually right, but there is a difference between the column names you use and those you describe in your tables structure.
Try changing it this way
SELECT t1.COURSE_NUMBER,
t1.SECTION_NUMBER,
t2.INSTRUCTOR,
t1.GRADE_A, t1.GRADE_B, t1.GRADE_C
FROM Grades t1
INNER JOIN
Sections t2
ON t1.COURSE_NUMBER = t2.COURSE_NUMBER AND
t1.SECTION_NUMBER = t2.SECTION_NUMBER
I added aliases to the tables to make it more compact, but obviously feel free to switch back to full tables names if you prefer.
SELECT Grades.COURSE_NUMBER, Grades.SECTION_NUMBER,Sections.INSTRUCTOR,Grades.A, Grades.B, Grades.C
FROM Grades,Sections
WHERE Grades.COURSE_NUMBER = Sections.COURSE_NUMBER
AND Grades.SECTION_NUMBER = Sections.SECTION_NUMBER
Please try this.
SELECT Grades.COURSE_NUMBER,
Grades.SECTION_NUMBER,Sections.INSTRUCTOR,Grades.A,
Grades.B, Grades.C
FROM Grades,Sections
WHERE Grades.COURSE_NUMBER = Sections.COURSE
AND Grades.SECTION_NUMBER = Sections.SECTION
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.