首页 > 数据库技术 > 详细

mysql三表查询sql语句

时间:2018-12-18 00:58:48      阅读:217      评论:0      收藏:0      [点我收藏+]
  1. 表结构:

Student学生表(学号、姓名、性别、年龄、编辑)

Course课程表(编号、课程名称)

sc选课表(选课编号、学号、课程编号、成绩)

(1)写一个SQL语句,查询选修了“计算机原理”的学生学号和姓名

(2)写一个SQL语句,查询“小明”同学选修的课程名称

(3)写一个SQL语句,查询选修了5门课程的学生学号和姓名

 

答案:

1

select student.stu_no,student.stu_name

from student,course,sc

where course.c_no=sc.c_no and sc.stu_no=student.stu_no and course.c_name=‘计算机原理‘;

2

select course.c_name,student.stu_name

from student,course,sc

where student.stu_name=‘小明‘ and student.stu_no=sc.stu_no and sc.c_no=course.c_no;

3

select student.stu_no,stu_name

from student,course,sc

where student.stu_no=sc.stu_no and sc.c_no=course.c_no

group by student.stu_no

having count(*)=5

 

建表及插入数据可参考如下语句:

create table student(

stu_no int,

stu_name varchar(10),

sex char(1),

age int(3),

edit varchar(20)

);

 

insert into student values

(1,‘wang‘,‘‘,21,‘hello‘),

(2,‘小明‘,‘‘,22,‘haha2‘),

(3,‘hu‘,‘‘,23,‘haha3‘),

(4,‘li‘,‘‘,25,‘haha4‘);

 

create table course(

c_no int,

c_name varchar(10)

);

 

insert into course values

(1,‘计算机原理‘),

(2,‘java‘),

(3,‘c‘),

(4,‘php‘),

(5,‘py‘);

 

create table sc(

sc_no int,

stu_no int,

c_no int,

score int(3)

);

 

insert into sc values

(1,1,1,80),

(2,2,2,90),

(3,2,1,85),

(4,2,3,70),

(5,2,4,95),

(6,2,5,89);

mysql三表查询sql语句

原文:https://www.cnblogs.com/reyinever/p/10134689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!