gpt4 book ai didi

php - 我的 WHERE 子句不符合条件

转载 作者:太空宇宙 更新时间:2023-11-03 12:09:27 25 4
gpt4 key购买 nike

使用连接(教师表和链接(桥)表)我正在尝试检索教师姓名

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com