Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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 如何从父表获取ID并从子表连接以运行查询。_Php_Mysql - Fatal编程技术网

Php 如何从父表获取ID并从子表连接以运行查询。

Php 如何从父表获取ID并从子表连接以运行查询。,php,mysql,Php,Mysql,我有两张桌子,一张是学生桌,另一张是学校桌。我正在使用FK将学校id(scid)调用到学生表 学生 +------+---------+-----+-----------+ | stid | student | age | school_id | +------+---------+-----+-----------+ | 1 | John | 26 | 2 | | 2 | Susan | 24 | 1 | +------+-------

我有两张桌子,一张是学生桌,另一张是学校桌。我正在使用FK将学校id(
scid
)调用到学生表

学生

+------+---------+-----+-----------+
| stid | student | age | school_id |
+------+---------+-----+-----------+
|  1   | John    |  26 |         2 |
|  2   | Susan   |  24 |         1 |
+------+---------+-----+-----------+
学校

+------+------------+-----------+
| scid | schoolname | syllabus  |
+------+------------+-----------+
|  1   | school1    | syllabus1 |
|  2   | school2    | syllabus2 |
+------+------------+-----------+
查看页面显示所有详细信息,其中我使用id从学生表获取详细信息,并使用联接从学校表获取信息

Student name: John
Age: 26
School: School 2
我需要为学生使用id,并使用join从学校获取名称。我该怎么做呢。我想出了这个,但是我怎么用这个id呢

SELECT schools.name
FROM schools
    LEFT JOIN students
        ON students.school_id=schools.scid;

您可以通过以下方式执行此操作:

select student, age, schoolname 
 from students stdu inner join schools sch 
on(stdu.school_id=sch.scid ) 
 where stdu.stid = 1
MySQL内部JOIN
子句将一个表中的行与其他表中的行相匹配,并允许您查询包含两个表中的列的行。


从学生stdu内部加入学校sch on(stdu.school\u id=sch.scid)中选择学生、年龄、学名,其中stdu.stid=1