Mysql 简单查询如何加入
第一桌(学生) 第二表(备注) 我已经提出了疑问, *所以输出是这样的* query=select a.*,b.从学生a的左连接备注b上(a.matricNo=b.matricNo) 但是我想要这样的输出:-Mysql 简单查询如何加入,mysql,Mysql,第一桌(学生) 第二表(备注) 我已经提出了疑问, *所以输出是这样的* query=select a.*,b.从学生a的左连接备注b上(a.matricNo=b.matricNo) 但是我想要这样的输出:- |name | matricNo | Subject | Grade | |John | A01 | History | A | |John | A01 | Math | B | |John | A01 |
|name | matricNo | Subject | Grade |
|John | A01 | History | A |
|John | A01 | Math | B |
|John | A01 | Language | NULL |
我已经尝试了左连接,右连接也提供了相同的输出。您可以像下面这样连接到学生表中的所有列
select t1.*,t2.grade from student t1
left join remark t2 on t1.name =t2.name and t1.matricno=t2.matricno and t1.subject =t2.subject
这正在起作用首先,您必须规范化数据库。您的第一个查询必须返回所需的结果以及更多。从学生a中选择a.*,b.成绩在a.name=b.name和a.matricNo=b.matricNo和a.Subject=b.Subject上左连接备注b。要获得所需内容,您需要有单独的主题表。
|name | matricNo | Subject | Grade |
|John | A01 | History | A |
|John | A01 | Math | B |
|name | matricNo | Subject | Grade |
|John | A01 | History | A |
|John | A01 | Math | B |
|John | A01 | Language | NULL |
select t1.*,t2.grade from student t1
left join remark t2 on t1.name =t2.name and t1.matricno=t2.matricno and t1.subject =t2.subject