zoukankan      html  css  js  c++  java
  • 条件/模糊/排序/范围/离散/聚合/分页/去重/分组查询---2017-04-09

    1.注释语法:--,#
    2.后缀是.sql的文件是数据库查询文件
    3.在数据库里面 列叫字段 行叫记录

    CRUD操作:
    create 创建(添加)
    read 读取
    update 修改
    delete 删除

    1、添加数据

    insert into 表名 values(值)

    insert into 表名(字段1,字段2) values(字段1值,字段2值)

    2、删除数据
    删除所有数据
    delete * from family
    删除特定的数据

    delete info from 表名 where 条件

    3、修改数据
    update * set 字段=字段值

    update 表名 set 要修改的内容 where 条件

    4、读取数据
    (1)简单读取,查询所有列(*) 所有行(没有加条件)
    select * from表名 
    (2)读取特定列
    select 要读取的内容 from 表名 where 条件
    (3)多条件查询
    select * from Info where 字段1=字段1值 or 字段2=字段2值 #或的关系
    select * from Info where 字段1=字段1值 and 字段2=字段2值  #与的关系
    (5)关键字查询(模糊查询)
    查所有包含奥迪的汽车
    select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
    查以'皇冠'开头的所有汽车
    select * from car where name like '皇冠%';
    查询汽车名称中第二个字符是'马'的
    select * from car where name like '_马%'; #下划线_代表任意一个字符
    (6)排序查询
    select * from car order by powers #默认升序排列
    select * from car order by powers desc #升序asc 降序 desc
    先按brand升序排,再按照price降序排
    select * from car order by brand,price desc

    (7)范围查询
    select * from car where price>40 and price<60
    select * from car where price between 40 and 60   注意:对于不同的数据库,可能会包括50,60,也可能不包括,也可能只包含1个

    (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(*) as 总数 from car#取所有的数据条数

    select sum(price) as 总和 from car #求价格总和
    select avg(price) from car #求价格的平均值
    select max(price) from car #求最大值
    select min(price) from car #求最小值

    注意: 取价格最高的车的全部信息: select *from car where price=(select max(price) from car);   

    (10)分页查询
    select * from car limit 0,10 #分页查询,跳过几条数据(0)取几条(10)
    规定一个每页显示的条数:m
    当前页数:n
    select * from car limit (n-1)*m,m  跳过(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

    特别注意:分组与去重的差别:表面上可能一样, 但是分组每一行都代表一个数组,一般会显示本组的第一个;

     

  • 相关阅读:
    绿色通用的网站后台系统管理模板
    本站源码免费下载-木庄网络博客
    expr 数字操作
    wget 实现web监控脚本
    wget 监控web服务器
    read + 计算
    判断字符串长度
    替换字符串
    shell 字符串操作
    shell 定义变量
  • 原文地址:https://www.cnblogs.com/chenguanai/p/6683949.html
Copyright © 2011-2022 走看看