zoukankan      html  css  js  c++  java
  • mysql 高级查询

    高级查询:
    1.连接查询
    select * from Info,Nation #这是两个表名,中间用逗号隔开
    形成笛卡尔积
    select * from Info,Nation where Info.nation=Nation.code

    select Info.code,Info.name,Info.sex,Nation.name as '民族',Info.birthday from Info,Nation where Info.nation=Nation.code

    select * from Info join Nation on Info.nation=Nation.code

    2.联合查询
    select code,name from Info
    union                           #这是两个表的行联合
    select code,name from Nation

    3.子查询
    子查询查询的结果作为父查询的条件

    (1)无关子查询:子查询执行的时候和父查询没有关系
    查民族为'汉族'的所有学生信息
    select * from Info where nation=(select code from nation where name='汉族')

    查询生产厂商为'一汽大众'的所有汽车信息
    select * from car where brand=()
    select brand_code from brand where prod_code=()
    select prod_code from productor where prod_name='一汽大众'


    select * from car where brand in(select brand_code from brand where prod_code in(select prod_code from productor where prod_name='一汽大众'))

    in代表有多种可能,当不确定的时候用。

    (2)相关子查询
    子查询在执行的时候需要用到父查询的内容

    查询汽车表中,汽车油耗小于该系列平均油耗的所有汽车信息

    select * from car where oil<(该系列平均油耗)
    select avg(oil) from car where brand =(该系列)

    select * from car a where oil<(select avg(oil) from car b where b.brand =a.brand)

     ab代表代号     a是外表的car,b是执行里面时候的car

  • 相关阅读:
    体验一下:AndroidX
    Android研发技术的进阶之路
    App 冷启动与热启动及启动白屏优化
    Android Q 正式命名为 Android 10
    Android开发学习路线的七个阶段和步骤
    安卓旅途之——开发数独(一)
    项目总结
    小组互评与自评
    典型用户与场景
    第二个Sprint计划
  • 原文地址:https://www.cnblogs.com/zxl89/p/5970075.html
Copyright © 2011-2022 走看看