如何获取具有条件的mysql查询结果

时间:2016-12-28 08:01:18

标签: php mysql

老师表

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

学生表

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

结果

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

结果是教师记录他的一个或多个学生得分> 0

如何查询?

感谢您的回答并抱歉我的英语不好

2 个答案:

答案 0 :(得分:3)

一个简单的子查询可以解决这个问题

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

也可以是INNER JOIN

答案 1 :(得分:1)

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

希望这对你有用。