zoukankan      html  css  js  c++  java
  • 查询

    查询

    select * from 表名称;

    select 字段1,字段2 from 表名称;

    distinct  去掉重复的关键字

    别名 select  t.字段1 from 表名称 t;

    where 语句

    举例:

           select name, job from emp;

    select distinct salary from emp;

           select name, salary+100 from emp where job = '开发'; 开发的工资加100

           select name, (salary + bonus) from emp; 工资和奖金的和统计出来

           select name, (salary+bonus) as t from emp; salary+bonus以别名t显示,as可以省略不写

           select name, (salary+bonus) t from emp where (salary+bonus)>5000; 此时where用别名会报错

    where后使用的符号

    >、<、>=、<=、and、or、not

    = 等于

    <> 不等于

    in:select * from emp where salary in(6000,4000);

    like:模糊查询,使用%表示多个占位符,_表示一个占位符

           例如select * from emp where name like '%le%';

    between…and

    例如select * from emp where salary between 4500 and 5500; 4500到5500之间

    order by * desc/asc

    注意:order by 必须放在select语句的末尾,

    例如select * from emp where gender = 'girl' order by salary desc, id desc;

    搜索女性,并排序,排序规则是以salary降序排列,salary一样时,id降序排列

    聚合函数

    聚合函数对一组值执行计算,并返回单个值,也被称为组函数,例如求数量、求和、求平均值、最大值、最小值。

    求数量---count()

    select count(*) from 表名称;

    select count(字段1) from 表名称 where (字段1+字段2) > 4000;

    求和---sum()

    select sum(字段1) from 表名称;

    select sum(字段1), sum(字段2)  from 表名称;

    select sum(字段1+字段2) from 表名称;

    select sum(字段1) + sum(字段2)  from 表名称;

    求平均值---avg()

    select avg(字段1) from 表名称;

    求最大/小值---max()/min()

     

    分组查询

    group by 是分组,并并未去重,将查询结果按一个或多个进行分组,字段值相同的为一组

    对订单中商品归类后,显示每一类商品的总价

    select product, sum(price) from tables group by product;

    查询购买了几类商品,并显示每类总价大于1000的商品及总价

    select product, sum(price) from tables group by product having sum(price) > 1000;

    注意:

    select product, sum(price) from tables where sum(price) > 1000 group by product

    不能这样写,因为where之前还未分类,查询的是每个产品的价钱,且where后不能加聚合函数

    查询购买了几类商品,并显示商品价格大于100,且每类总价大于1000的商品及总价

    select product, sum(price) from tables where price > 100 group by product having sum(price) > 1000;

    ==>上面语句的流程:先查询单价大于100的商品,在分组,分组后显示总价大于1000的商品类及其大于100商品的总价

  • 相关阅读:
    Centos7 搭建FTP服务
    Mitmproxy 安装
    NET Reflector 8 使用
    web sevice 生成代理类及使用
    WCF 动态生成 不用增加引用两种方式
    oracle 表空间、用户名 相关语句
    恢复24小时之内删除的表及表数据
    pl/sql插入报错
    jquer ajax
    pdf增加水印
  • 原文地址:https://www.cnblogs.com/like1824/p/13368581.html
Copyright © 2011-2022 走看看