zoukankan      html  css  js  c++  java
  • MySQL增删改查之查询

    (7)范围查询
    select * from car where price>40 and price<60   --查询价格在40-60之间的
    select * from car where price between 40 and 60   --between...and...

    (8)离散查询       查询离散值,例如查询汽车价格是30、40、50、60等整数的
    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)   --价格除30,40,50,60以外的数的

    (9)聚合函数(统计查询)
    select count(*) from car    --查询这张表里面有多少数据。count方法可以用来求条数
    select count(code) from car #取所有的数据条数。code为主键,内容不为空,可以用code查询。
    select sum(price) from car #求价格总和
    select avg(price) from car #求价格的平均值
    select max(price) from car #求最大值
    select min(price) from car #求最小值

      数据查询原理:数据库在查询数据的时候会每一条数据都会查一遍,查询的时候如果条件成立,会返回数据true,如果不成立,会返回false。如果返回的是true,就会选取这条数据,如果返回的是false,就pass掉这条数据。例如,如果select * from car where 1=1,输出的会是表格的所有数据,因为1=1恒满足条件。

    (10)分页查询   每一页有很多数据,可以查看下一页的数据。
    select * from car limit 0,10 #分页查询,跳过几条数据(0)  取几条(10)
    规定一个每页显示的条数:m
    当前页数:n
    select * from car limit (n-1)*m,m  取第n页显示的m条数据

    (11)去重查询
    select distinct brand from car     --distinct表示去重,前面代码表示对brand列去重查询

    去重查询适合查1列,查2列就不适合用了。

    (12)分组查询
    查询汽车表中,每个系列下汽车的数量。按照brand分组。
    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

  • 相关阅读:
    java基础-基本的输入与输出
    Java基础-位运算符Bitwise Operators
    Java基础-字符串连接运算符String link operator
    Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators
    Java基础-逻辑运算符Logic Operators
    Java基础-比较运算符Compare Operators
    Java基础-算术运算符(Arithmetic Operators)
    NGUI裁剪模型和粒子
    编辑器插件数据保存之Serializable
    using语法糖
  • 原文地址:https://www.cnblogs.com/Strive-count/p/5968924.html
Copyright © 2011-2022 走看看