简体   繁体   中英

How get the result of mysql query with condition

teacher table

teacher_id      teacher_name
============================
1               john
2               doe

student table

student_id      student_name    student_score   teacher_id
==========================================================
1               amelia          0               1
2               anderson        0               1
3               fabiano         0               1
4               ronaldo         0               2
5               peter           6               2
6               alex            0               2

result

teacher_id      teacher_name
============================
2               doe

the result is teacher record who one or more of his student score > 0.

How the query of this ?

thanks for the answer and sorry for my bad english

A simple subquery does the trick

SELECT * FROM teacher WHERE teacher_id IN
(SELECT teacher_id FROM student WHERE student_score > 0)

Also possible is an INNER JOIN

SELECT teacher.* FROM teacher
LEFT JOIN student as student teacher.teacher_id = student.teacher_id
WHERE student.student_score > 0

Hopefully this will works for you.

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