zoukankan      html  css  js  c++  java
  • sql多表查询--学生课程表查询

    创建表

    学生表:(序号,姓名,年龄,专业)

    create table s(
    sno INT(11) auto_increment,
    sname varchar(125),
    sage INT(11),
    sdept varchar(125),
    primary key(sno)
    );
    

     课程表:(序号,课程名)

    create table c(
    cno INT(11) auto_increment,
    cname varchar(255),
    primary key(cno)
    );
    

     学生课程关系表:(序号,学生序号,课程序号,分数)

    create table sc(
    scno INT(11) auto_increment,
    sno INT(11),
    cno INT(11),
    grade INT(255),
    primary key(scno)
    );
    

    常用单表查询

    limit

    一个参数是查询条数

    select sno,sname from s limit 2
    

    两个参数n,m是(1.从第n+1行开始2.查询m条数)

    select sno,sname from s limit 0,2
    

     

    between

    select  sno,sname,sage from s where sage between 18 and 20
    

     

     distinct

     不重复查询

    select distinct sdept from s 
    

    like

    通配符查询

    select  sname from s where sname like "小%"
    

     

    多表查询

    in

     1.查询修读“软件工程”的学生姓名(使用in嵌套子查询)

    select sname from s where sno in
    (select sno from sc where cno in (
    	select cno from c where cname="软件工程"
    ))
    

     2.查询至少修读“软件工程”与“c语言”的学生姓名(在1中使用or)

    select sname from s where sno in 
    (select sno from sc where cno in 
    (select cno from c where cname="软件工程" or cname="c语言")) 
    

     3.查询不修“软件工程”的学生姓名(在2中使用not)

    select sname from s where sno in 
    (select sno from sc where cno not in 
    (select cno from c where cname="软件工程" )) 
    

     exits

  • 相关阅读:
    jQuery 插件开发——Menu(导航菜单)
    jQuery 插件开发——PopupLayer(弹出层)
    jQuery 插件开发——GridData(表格)
    angularjs+requlirejs 搭建前端框架(1)
    探讨js闭包
    python的特殊方法介绍
    收集TCP端口的访问延迟和丢包率
    【IT界的厨子】酱香鲈鱼
    Centos 6.5 安装Python 3.7
    python技巧 python2中的除法结果为0
  • 原文地址:https://www.cnblogs.com/ming-szu/p/8966930.html
Copyright © 2011-2022 走看看