zoukankan      html  css  js  c++  java
  • CRUD操作(20161017)

    上午:

     (7)范围查询

    select * from car where price>40 and price<60

    select * from car where price between 40 and 60

    (8)离散查询

    select * from car where price=30 or price=40 or price=50 or price=60;

    select * from car where price in(30,40,50,60)

    select * from car where price not in(30,40,50,60)

    (9)聚合函数(统计查询)

    select count(*) from car

    select count(code) from car #取所有的数据条数

    select sum(price) from car #求价格总和

    select avg(price) from car #求价格的平均值

    select max(price) from car #求最大值

    select min(price) from car #求最小值

    (10)分页查询

    select * from car limit 0,10  #分页查询,跳过几条数据(0)取几条(10)

    规定一个每页显示的条数:m

    当前页数:n

    select * from car limit (n-1)*m,m

    (11)去重查询

    select distinct brand from car

    (12)分组查询

    查询汽车表中,每个系列下汽车的数量

    select brand,count(*) from car group by brand

    分组之后,只能查询该列或聚合函数

    取该系列价格平均值大于40的系列代号

    select brand from car group by brand having avg(price)>40

    取该系列油耗最大值大于8的系列代号

    select brand from car group by brand having max(oil)>8

     下午:

    高级查询:
    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=(select prod_code from productor where prod_name='一汽大众'))

    (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)

  • 相关阅读:
    十大经典排序算法最强总结(含Java、Python码实现)
    数据库查询优化的12种方式
    开发环境、测试环境、预发布环境、生产环境的区别
    阅站无数的我,只推荐下面这些让你起飞的
    访问控制符
    继承的意义
    数组继承的意义
    java 俄罗斯方块
    类和面向对象
    随机生成数组游戏
  • 原文地址:https://www.cnblogs.com/zsczsc/p/5983803.html
Copyright © 2011-2022 走看看