Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySQL中的3个表中选择几个字段,但该字段不能要求第三个表中存在的字段_Php_Mysql_Sql - Fatal编程技术网

Php 从MySQL中的3个表中选择几个字段,但该字段不能要求第三个表中存在的字段

Php 从MySQL中的3个表中选择几个字段,但该字段不能要求第三个表中存在的字段,php,mysql,sql,Php,Mysql,Sql,我有三个表,分别是student、student_lessons和student_image。我想选择学生、学生课程并显示学生图像(如果存在)。在一个查询中是否仍然可以处理它 尝试选择学生id、学生姓名、学生课程(必须存在)和学生图像路径、学生图像图片id(如果存在) # TABLE `students` id | name | surname 1 : John : Malkovich 2 : John : Smith # TABLE `students_lessons` ( no AI

我有三个表,分别是student、student_lessons和student_image。我想选择学生、学生课程并显示学生图像(如果存在)。在一个查询中是否仍然可以处理它

尝试选择学生id、学生姓名、学生课程(必须存在)和学生图像路径、学生图像图片id(如果存在)

# TABLE `students`
id | name | surname
1  : John : Malkovich
2  : John : Smith

# TABLE `students_lessons` ( no AI id here)
student_id | lesson | note
1          : math   : A
1          : geo    : B
2          : math   : C

# TABLE `students_image`
pic_id | student_id | image_path 
1      :  1         : some path       
2      :  1         : some path
3      :  2         : some path   
如果我尝试使用joint进行查询,如果找不到任何图片,它将返回零。如果单个查询中存在图像,如何进行查询以选择图像

SELECT students.* 
FROM students
JOIN students_lessons ON (students_lessons.student_id = students.id)
LEFT JOIN students_image ON (students_image.student_id = students.id)
或者,如果您只关心图像是否存在:

SELECT students.* 
FROM students
JOIN students_lessons ON (students_lessons.student_id = students.id)
WHERE EXISTS (
   SELECT si.* 
   FROM students_image si
   WHERE si.student_id = students.id
)
我想你要找的是
左连接
。只需修改
选择
链接即可。此查询选择所有学生和有课的学生,无论他们是否有图像