zoukankan      html  css  js  c++  java
  • mysql基础(二)

    0x02数据查询

    2、连接查询

    • 同时涉及两个以上的表的查询
    • 等值连接:连接运算符为=
      ①查询每个学生及其选修课程的情况
    select student.*,sc.* from student,sc where student.sno=sc.sno;
    

    • 自然连接:
      ②查询每个学生及其选修课程的情况
    select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno=sc.sno
    


    ③查询选修2号课程且成绩在90分以上的所有学生的学号和姓名。

    select student.sno,sname from student,sc where student.sno and sc.cno='2' and sc.grade>90;
    

    • 多表连接:两个以上的表进行连接
      ④查询每个学生的学号,姓名,选修的课程名及成绩
    select student.sno,sname,cname,grade from student,sc,course where student.sno=sc.sno and sc.cno=course.cno;
    

    3、嵌套查询

    • 嵌套查询:将一个查询块嵌套在另一个查询块的where子句或having短语的条件中的查询称为嵌套查询。
      ⑤选择课程号为2的学生的名字
    select sname from student where sno in (select sno from sc where cno='2');
    


    ⑥查询与“刘晨”在同一个系学习的学生

    select sno,sname,sdept from student where sdept in (select sdept from student where sname="刘晨");
    


    ⑦查询选修了课程名为“信息系统”的学生学号和姓名

    select sno,sname from student where sno in (select sno from sc where cno in (select cno from course where cname='信息系统'));
    #先在course关系中找出“信息系统”的课程号,为3号
    #然后在sc关系中找出选修了3号课程的学生学号
    #最后在student关系中取出sno和sname
    


    ⑧用连接查询实现

    select sno,sname from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and course.cname='信息系统';
    

    4、集合查询
    集合操作的种类

    • 并操作union
    • 交操作intersect
    • 差操作except
      参加集合操作的各查询结果的列数必须相同,对应项的数据类型也必须相同

    ①查询计算机科学系的学生及年龄不大于19岁的学生

    select * from student where sdept='cs' union select select * from student where sage<=19;
    #union:将多个查询结果合并起来时,系统自动去掉重复元组
    #union all:将多个查询结果合并起来时,保留重复元素
    


    ②查询选修了课程1或者选修了课程2的学生

    select sno from sc where cno='1' union select sno from sc where cno='2';
    


    ③查询计算机科学系的学生与年龄不大于19岁的学生的交集。

    select * from student where sdept='cs' intersect select * from student where sage<=19;
    #爆出语法错误(待解决)
    

    ④实际上就是查询计算机科学系中年龄不大于19岁的学生。

    select * from student where sdept='cs' and sage<=19;
    

  • 相关阅读:
    下载PDF格式的Html
    EnumHelper.cs
    FtpHelper.cs
    Windows下用cmd命令安装及卸载服务
    c# list排序
    用NOPI将图片二进制流导出到Excel
    jquery的clone方法 于textarea和select的bug修复
    时间戳格式化函数
    快速激活Navicat Premium 12
    防止应用程序重复打开
  • 原文地址:https://www.cnblogs.com/observering/p/13782381.html
Copyright © 2011-2022 走看看