select * from student,score where student.id = score.student_id;
最后补下其他条件即可
复制代码
select student.name,score.score from student,score where student.id = score.student_id and student.name = '许仙';
join on 写法
复制代码
select student.name,score.score from student join score on student.id = score.student_id and student.name = '许仙';
注意: 如果在实际开发中 谨慎使用笛卡尔积 如果表中数据很大,容易把mysql弄崩溃
(2)查询所有同学的总成绩,及同学的个人信息:
成绩在score表 个人信息在学生表
总成绩需要使用到聚合函数 可以按照学生名字进行分组
复制代码
select * from student,score where student.id = score.student_id group by student.name;
进行求和 并且留下指定列
复制代码
select student.name,sum(score) as '总成绩' from student,score where student.id = score.student_id group by student.name;
join on 写法:
复制代码
select student.name,sum(score) as '总成绩' from student join score on student.id = score.student_id group by student.name;
3)查询所有同学的成绩,及同学的个人信息:
列出 同学的名称 课程的名字 以及课程的成绩
提取关键词:成绩在score表 同学在student表 个人信息在course表
复制代码
select * from student,course,score;
数据相当大 对它进行细节划分
复制代码
select * from student,course,score where student.id = score.student_id and score.course_id = course.id;
对列进行精简即可
复制代码
select student.name as '学生名称' , course.name as '科目名称' , score.score from student,course,score where student.id = score.student_id and score.course_id = course.id;
join on 写法
复制代码
select student.name as '学生名称' , course.name as '科目名称' , score.score from student join course on student.id = course.id join score on score.course_id = course.id;