MySQL如何从引用的表中检索值?
我正在尝试学习各种表中字段之间的引用关系,但我很难理解以下内容: 如果我有两张桌子: 1) 一种是针对有姓名、年龄、id、gpa和年级的学生 2) 一种是教师点名,上面有班级名称、组号和上课学生的ID 如何编写类似于以下伪代码的查询:MySQL如何从引用的表中检索值?,mysql,Mysql,我正在尝试学习各种表中字段之间的引用关系,但我很难理解以下内容: 如果我有两张桌子: 1) 一种是针对有姓名、年龄、id、gpa和年级的学生 2) 一种是教师点名,上面有班级名称、组号和上课学生的ID 如何编写类似于以下伪代码的查询: SELECT classname and sectionnumber WHERE the students in the class at least junior? 如何使用一个表中的字段引用另一个被引用表中的字段值 select a.cla
SELECT classname and sectionnumber WHERE the students in the class at least junior?
如何使用一个表中的字段引用另一个被引用表中的字段值
select a.classname, a.sectionNo
from ClassRosert a
inner join Student b using(studentId)
where b.class = 'junior';
或者在那种情况下你可以这样做
select a.classname, a.sectionNo
from ClassRosert a, Student b
where a.studentId = b.studentId
and b.class = 'junior';
但我会推荐第一个,因为更清楚的是,你们正在加入谈判桌
注意,在第一种情况下,我使用了
using
关键字,因为映射值在两个表中具有相同的名称(studentId)。但是,如果在student表中它只被称为ID或任何其他名称,则联接将是内部联接student b on a.studentid=b.ID
在选择后添加distinct关键字以避免重复<代码>选择不同的代码>