zoukankan      html  css  js  c++  java
  • 数据库---查询语句(三):高级查询

    高级查询

    一.多表连接(连接的是两个表中的列)

    1.select * from Info,Nation where Info.Nation=Nation.Code

     select  Info.Code,Info.Name,Nation.Name  from Info ,Nation where Info.Nation=Nation.Code    //where 后面是一个外键关系

     select * from Info      出现的现象,形成的表叫做笛卡尔积

    2. join 连接

     select * from Info join Nation  on  Info.Nation=Nation.Code    (join on 语法   on 后面跟条件)       //join 也可能会形成笛卡尔积,大数据不建议用

    二.多表联合(扩展行)

    select * from  Info  where Code='p001'

    select * from Info where Nation='n001'

    两条结果放到一起→用union连接:

    select * from Info  where Code='p001' union select * from Info where Nation='n001'

    注:多表联合有个要求--查询的列数要求一样

    三.子查询:无关子查询

    (有两个查询语句,其中一个查询的结果被另一个用,那么被用的查询叫做子查询,用的那个查询叫做附查询)

    1. select Code  from  Nation  where  Name='汉族'

        select * from Info where  Nation='n001'

        → →select * from Info where  Nation=(select Code from Nation where Name='汉族')    

         //括号里面的查询结果,当做外面查询的条件,所以里面的查询叫做子查询(也叫里层查询),外面的查询叫做复查询(也叫外层查询)

        //上面的信息如果要除了这条数据之外,其他的数据,可以用“ != ”

    2. select * from Info where Nation in (select Code  from Nation where Name='汉族' or Name='苗族')

         //子查询查的不是一条数据,而是多条数据的话,用in 链接

    3. select * from Info where Nation not in (select Code  from Nation where Name='汉族' or Name='苗族')

        //不要这两条数据

    四.子查询:相关子查询(里层查询需要用到外层查询的条件)

      select * from Car a  where a.Oil <(select avg(Oil) from Car b where b.Brand = a.Brand) 

    (括号里相当于--select * avg (oil) from car where brand='b001')

  • 相关阅读:
    Linux的inode的理解
    linux中ctrl+z和ctrl+c的区别
    linux后台运行和关闭、查看后台任务
    解决Could not get lock /var/cache/apt/archives/lock
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
    【免费】某平台16980元编程课程资料下载,仅此1次
    秒杀系统架构分析与实战,一文带你搞懂秒杀架构!
    阿里数据库大牛的 MySQL 学习指南!
    Intellij IDEA 撸码最头大的问题。。
    Java 中的 SPI 机制是什么鬼?高级 Java 必须掌握!
  • 原文地址:https://www.cnblogs.com/supermeimei/p/5137855.html
Copyright © 2011-2022 走看看