简体   繁体   中英

MySQL query to find if a value of one column in one table is between two values in two columns on another table

Long title question, so my apologies for that! I have two tables, one is as follows:

Student Name         Grade
John Doe             96
John Foe             65
Dan Doe              76
Mary Doe             85

The other table, is as follows:

Grade Start      Grade End       Status
0                60              Bad Student
61               70              OK Student
71               80              Good Student
81               90              Great Student
91               100             Honor Student

I am trying to create a MySQL view that will pull the student's grade, and tell me the status of that student such as:

Student Name     Grade      Status
John Doe         90         Honor Student
John Foe         65         OK Student

So on and so forth. I can't think of a query that will give me that information. I'm at a complete loss with the query, any help?

SELECT student_name, student_grade from student_grade_table WHERE ???

Use a join:

SELECT s.student_name, s.grade, g.status
FROM students AS s
JOIN grades as g ON s.grade BETWEEN g.grade_start AND g.grade_end

DEMO

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM