0423-mysql查询语句大全(待补充)

建表、数据插入代码:

#新建学生表
drop table if exists student;
create table student(
    sno varchar(20) not null primary key comment '学号',
    sname varchar(20) not null comment '学生姓名',
    ssex varchar(20) not null comment '学生性别',
    sbirthday Datetime comment '学生出生年月',
    class varchar(20) comment '学生所在班级'
);
#给学生表中插入数据
insert into student values(
    '108','曾华','','1977-09-01','95033'),(
    '105','匡明','','1975-10-02','95031'),(
    '107','王丽','','1976-01-23','95033'),(
    '101','李军','','1976-02-20','95033'),(
    '109','王芳','','1975-02-10','95031'),(
    '103','陆君','','1974-06-03','95031');
#新建课程表
drop table if exists course;
create table course(
    cno varchar (20) not null primary key comment '课程号',
    cname varchar (20) not null comment '课程名称',
    tno varchar (20) not null comment '教工编号'
);
#给课程表中插入数据
insert into course values
('3-105','计算机导','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','831');
#新建成绩表
drop table if exists score;
create table score(
    sno varchar (20) not null comment '学号',
    cno varchar (20) not null comment '课程号',
    degree Decimal(4,1) comment '成绩',
    primary key(sno,cno)
);
#给成绩表中插入数据
insert into score values
('103','3-245',86),
('105','3-245',75),
('109','3-245',68),
('103','3-105',92),
('105','3-105',88),
('109','3-105',76),
('101','3-105',64),
('107','3-105',91),
('108','3-105',78),
('101','6-166',85),
('107','6-166',79),
('108','6-166',81);
#新建教师表
drop table if exists teacher;
create table teacher(
    tno varchar (20) not null primary key comment '教工编号',
    tname varchar (20) not null comment '教工姓名',
    tsex varchar (20) not null comment '教工性别',
    tbirthday datetime comment '教工出生年月',
    prof varchar (20) comment '职称',
    depart varchar (20) not null comment '教工所在部门'
);
#给教师表中插入数据
insert into teacher values
('804','李诚','','1958-12-02','副教授','计算机'),
('856','张旭','','1969-03-12','讲师','电子工程'),
('825','王萍','','1972-05-05','助教','计算机系'),
('831','刘冰','','1977-08-14','助教','电子工程');
建表、数据插入代码

开始查询:

#like:姓王的
select  * from teacher  where tname like '王%';
#like:第二个字是冰的
select * from teacher where tname like '_冰%';
#1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student;
/*2、 查询教师所有的单位即不重复的Depart列。
all:允许出现——默认不写就是All(允许的)。
distinct:不允许出现——就是所谓的“消除重复行”。*/
select distinct depart from teacher ;
select count(distinct depart) from teacher;
#3、 查询Student表的所有记录。
select * from student;
#4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree >= 60 and degree <= 80;
select * from score where degree between 60 and 80;
#5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree = 85 or degree = 86 or degree = 88;
select * from score where degree in(85,86,88);
#6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class = '95031' or ssex = '';
#7、 以Class降序查询Student表的所有记录。
select * from student order by class desc;
# 8、以Cno升序、Degree降序查询Score表的所有记录。
#order by 字段名(默认升序);desc:降序
select * from score order by cno,degree desc;
like/distinct/between..and/in/or/order by/

猜你喜欢

转载自www.cnblogs.com/flypea93/p/8911973.html