使用连接(教师表和链接(桥)表)我正在尝试检索教师姓名
MySQL 表
1.) 我有一个链接(桥)表:我在这里插入关系
Table Name "cl_st_tch"
+-------------+-------------+-------------+---------------+
| classes_id | students_id | teachers_id | company_id |
+-------------+-------------+-------------+---------------+
| 1 | 1 | 2 | 1 |
| 1 | 2 | 4 | 1 |
| 1 | 3 | 4 | 1 |
| 1 | 4 | 4 | 1 |
| 1 | 5 | 4 | 1 |
+-------------+-------------+-------------+---------------+
2.) 这是我的教师表:存储教师名字的简单表
Table Name "teachers"
+-------------+-------------+
|id | name |
+-------------+-------------+
| 1 | Sandra |
| 2 | Alex |
| 3 | Kinder |
| 4 | Michael |
+-------------+-------------+
MySQL查询
SELECT
DISTINCT teachers.name, teachers.id, cl_st_tch.teachers_id
FROM teachers, cl_st_tch
WHERE teachers.id && cl_st_tch.teachers_id = 4
“期望的”结果
我希望这只会产生 1 个 Name = Michael
“不希望的”结果
相反,我得到了所有老师的名字
桑德拉
亚历克斯
健达
迈克尔
我错过了什么?任何帮助将不胜感激!
最佳答案
SELECT
DISTINCT teachers.name, teachers.id, cl_st_tch.teachers_id
FROM teachers
INNER JOIN cl_st_tch
ON teachers.id = cl_st_tch.teachers_id
WHERE teachers.id = 4
关于php - 我的 WHERE 子句不符合条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25025436/