MySQL高级查询语句

1. limt, order by
    select * from (表的名称) limit 3;  #查询某个表的前三列
    order by (字段名) asc/desc子句:将查询结果按某个字段升序/降序排列
2. like / not like , in
    like子句:用于判断字符串格式是否相符,%代表任意字符串
    -- 查询名字中有王的名字
    select * from tbstudent where stuname like "%王%";
    in子句:判断是否落在某个范围内
    select * from tbstudent where stuname in ("张三丰", "郭靖","杨过")
3. count, max, min, avg, sum
    select count(*) from tbstudent; #统计次数
    select max(*) from tbstudent; #最大
    select min(*) from tbstudent; #最小
    select avg(*) from tbstudent; #平均值
    select sum(*) from tbstudent; #求和
4. group by, having
    group by子句,分组条件,分组势必对应着某种统计结果
    select count(*), scdate from tbsc group by scdate;
    having子句:对分组后的结果(二手数据)进行条件筛选
    where是对分组前的数据(一手数据)进行条件筛选
    -- 查询所有女同学的姓名,性别,和出生日期
    select stuname, stubirth from tbstudent
    where stusex = 0 ;
5.练习
    创建数据库
    show databases ;
    drop database if exists srs;
    create database srs default charset utf8;
    use srs ;
    drop table if exists tbstudent;
    创建学生表
    create table tbstudent(
    stuid integer not null,
    stuname varchar(20) not null,
    stusex bit default 1,
    stubirth datetime not null,
    stutel char(11),
    stuaddr varchar(255),
    stuphoto longblob,
    primary key(stuid)
    );
    修改学生表删除studel 这一列
    alter table tbstudent drop column stutel;
    desc tbstudent;
    创建课程表
    drop table if exists tbcourse;
    create table tbcourse(
    cosid integer not null,
    cosname varchar(50) not null,
    coscredit tinyint not null,
    cosintro varchar(255)
    );
    给课程表指定主键
    alter table tbcourse add constraint pk_course primary key (cosid);
    创建学生选课记录表
    create table tbsc (
    scid integer primary key auto_increment,
    sid integer not null,
    cid integer,
    scdate datetime not null,
    score float
    );
    给表tbsc添加外检约束
    alter table tbsc add constraint fk_sid foreign key (sid) references tbstudent (stuid) on delete cascade on update cascade;
    alter table tbsc add constraint fk_cid foreign key (cid) references tbcourse (cosid) on delete set null on update cascade;
    添加学生记录
    insert into tbstudent values (1001, '张三丰', default, '19780101', '成都市一环路西二段17号', null);
    insert into tbstudent (stuid, stuname, stubirth)  values (1002, '郭靖', '19800202');
    insert into tbstudent (stuid, stuname, stusex, stubirth, stuaddr)  values (1003, '黄蓉', 0,  '19820303', '成都市二环南路四段123号');
    insert into tbstudent values (1004, '张无忌', 1, '19900404', null , null); 
    insert into tbstudent values (1005, '丘处机', 1, '19830505', '北京市海淀区盛北里西区28号' ,null); 
    insert into tbstudent values (1006, '王处一', 1, '19850606', '深圳市宝安区宝安大道5010号' ,null); 
    insert into tbstudent values (1007, '刘处玄', 1, '19870707', '郑州市金水区纬五路21号' ,null); 
    insert into tbstudent values (1008, '孙不二', 0, '19890808', '武汉市光谷大道61号' ,null); 
    insert into tbstudent values (1009, '平一指', 1, '19920909', '西安市雁塔区高新六路52号',null); 
    insert into tbstudent values (1010, '老不死', 1, '19931010', '广州市天河区元沿路310号',null); 
    insert into tbstudent values (1011, '王大锤', 0, '19941111', null ,null); 
    insert into tbstudent values (1012, '隔壁老王', 1, '19951212', null ,null); 
    insert into tbstudent values (1013, '郭啸天', 1, '19771025', null ,null); 
    删除学生记录
    delete from tbstudent where stuid=1004;
    更新学生记录
    update tbstudent set stubirth='19801212',

    stuaddr= '上海市宝山区同济支路199号' where stuid=1002;
    添加课程表记录
    insert into tbcourse values
    (1111,'c语言程序设计', 3 , '大神级讲师授课需要抢座'),
    (2222, 'java程序设计', 3 , null),
    (3333, '数据库概论', 2 , null),
    (4444, '操作系统原理', 4 , null);
    添加学生选课表记录
    INSERT INTO tbsc VALUES 
    (DEFAULT, 1001, 1111, '2016-9-1', 95),
    (DEFAULT, 1002, 1111, '2016-9-1', 94),
    (DEFAULT, 1001, 2222, NOW(), NULL),
    (DEFAULT, 1001, 3333, '2017-3-1', 85),
    (DEFAULT, 1001, 4444, NOW(), NULL),
    (DEFAULT, 1002, 4444, NOW(), NULL),
    (DEFAULT, 1003, 2222, NOW(), NULL),
    (DEFAULT, 1003, 3333, NOW(), NULL),
    (DEFAULT, 1005, 2222, NOW(), NULL),
    (DEFAULT, 1006, 1111, NOW(), NULL),
    (DEFAULT, 1006, 2222, '2017-3-1', 80),
    (DEFAULT, 1006, 3333, NOW(), NULL),
    (DEFAULT, 1006, 4444, NOW(), NULL),
    (DEFAULT, 1007, 1111, '2016-9-1', NULL),
    (DEFAULT, 1007, 3333, NOW(), NULL),
    (DEFAULT, 1007, 4444, NOW(), NULL),
    (DEFAULT, 1008, 2222, NOW(), NULL),
    (DEFAULT, 1010, 1111, NOW(), NULL);

    -- 1.查询所有学生的信息
    select * from tbstudent;

    -- 2.查询所有课程及学分
    select cosname,coscredit from tbcourse;

    -- 3.查询所有女同学的姓名和出生日期
    select stuname, stubirth from tbstudent
    where stusex = 0 ;

    -- 4 查看所有80后学生的姓名,性别,出生日期
    select stuname, stusex, stubirth from tbstudent
    where stubirth between '19800101' and '19891231';

    -- 5 查询姓王的学生姓名和性别
    select stuname, stusex from tbstudent where stuname like
    '王%'

    -- 6 查询姓郭名字总共两个字的学生的姓名
    select stuname from tbstudent where stuname like "郭_" ;

    -- 7 查询姓郭名字总共三个字的学生的姓名
    select stuname from tbstudent where stuname like "郭__" ;

    -- 8 查询有王字的学生的姓名
    select stuname from TbStudent where stuname like '%王%';
    -- 9查询没有录入家庭地址和照片的学生的姓名
    select stuname from tbstudent where stuaddr is null and stuphoto is null;

    -- 10 查询学生选课的所有日期(去重)
    select distinct scdate from TbSC;
    -- 11 查询学生的姓名和生日按年龄从大到小排序
    select stuname, stubirth from tbstudent order by stubirth asc; 

    -- 12.查询所有录入了家庭住址的男学生的姓名、出生日期和家庭住址按年龄从小到大排列
    select stuname, stubirth, stuaddr from tbstudent
    where stuaddr is not null and stusex = 1

    -- 13 查询年龄最大的学生的出生日期(聚合函数)
    select min(stubirth),stuname from tbstudent ;

    -- 14 查询年龄最小的学生的出生日期(聚合函数)
    select max(stubirth) from TbStudent;
    -- 15 查询男女学生的人数(分组和聚合函数)
    select stusex, count(*) stusex from tbstudent group by stusex;

    -- 16 查询课程编号为1111的课程的平均成绩(筛选和聚合函数)
    select avg(score) from tbsc where cid=1111

    -- 17 查询学号为1001的学生所有课程的总成绩(筛选和聚合函数)
    select sid, sum(score) from tbsc
    where sid = 1001;

    -- 18 查询每个学生的学号和平均成绩, null值处理为0(分组和聚合函数)
    select sid as `学号`, ifnull(avg(score), 0) as
     `平均成绩` from TbSC group by sid;

    -- 19 查询平均成绩大于等于90分的学生的学号和平均成绩
    select sid as `学号`, avg(score) as `平均成绩` from TbSC 
    group by sid having avg(score)>=90;

    -- 20.查询年龄最大的学生的姓名(子查询)
    select stuname, min(stubirth) from tbstudent;

    -- 21.查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
    select stuname from TbStudent where stuid in 
    (select sid from TbSC group by sid having count(sid)>2);

    -- 22.查询选课学生的姓名和平均成绩(子查询和连接查询)
    select stuname, avgscore from TbStudent t1 inner join
    (select sid, avg(score) as avgscore from TbSC 
    where score is not null group by sid) t2
    on t1.stuid=t2.sid;

    -- 23.查询学生姓名、所选课程名称和成绩(连接查询)
    select stuname, cosname, score from 
    TbStudent t1, TbCourse t2, TbSC t3
    where t1.stuid=t3.sid and t2.cosid=t3.cid and 
    t3.score is not null;

    -- 24.查询每个学生的姓名和选课数量(左外连接和子查询)
    select stuname, count(sid) from tbstudent
    left join tbsc 
    on sid=stuid
    group by stuid;

猜你喜欢

转载自blog.csdn.net/random39/article/details/79983129