zoukankan      html  css  js  c++  java
  • SQL模糊查询,sum,AVG,MAX,min函数

    cmd
    
    mysql -hlocalhost -uroot -p
    
    select * from emp where ename like '___'  -- 三个横线, - 代表字符,可以查询 三个ename是3个字符数据
    

    select * from emp where job like '___员'; -- job是四个字符且以 员 字结尾
    
    select * from emp where ename like 'm%'; --查询 ename 以 m 开头的记录,% 匹配 0 - n个任何字符
    select * from emp where ename like '_u%';  -- 查询 ename 中第二个字符为 u 的记录
    select * from emp where ename like '%s%'; -- 查询 ename 中包含 s 的记录
    


    IFNULL(expr1,expr2),如果 expr1 为 null ,则 expr1 = expr2

    select *,IFNULL(score1,0) + IFNULL(score2,0) form emp;   --如果score1为null ,则score1 = 0 
    


    常用聚合函数

    count

    select count(*) from emp;   -- 统计记录数
    select count(ename) from emp; -- 统计ename 记录数,该记录字段为NULL则不统计在内
    
    -- 统计员工表总salary大于 2500 的人数
    select count(*)  from  emp where salary > 2500;
    
    -- 统计 salary 与 performance 之和大于 5000 的人数
    select count(*) from emp where salart + IFNULL(performance,0) > 5000 ; --  performance可能会为null,如果为null ,就替换为 0
    

    sum AVG MAX min

    -- 查询 salary 之和
    select sum(salary) from emp;
    
    -- 查询 salary之和,performance之和
    select sum(salary), sum(performance) form emp;
    
    -- 查询所有 salary + performance 的和
    select * from sum(salart + IFNULL(performance,0)) form emp;
    
    --  求 salary 的评价值
    select AVG(salary) from emp;
    
    -- 求最大salary  最小salary
    select MAX(salary),MIN(salary) from emp;
    

    sql 分组查询 group by

    分组查询:将查询结果按一个或多个字段进行分组,字段值相同的为一组

    分组使用:

    • 当 group by 单独使用时,只显示每组的第一条记录,所以,group by 单独使用意义不大
    select * from emp GROUP BY gender;
    

  • 相关阅读:
    Object之总结(一)
    Object之registerNatives
    Object之finalize
    阿里腾讯百度360
    Object之getClass
    Object之clone
    Object之toString
    Object之notify
    Object之wait
    Object之equals与hashCode
  • 原文地址:https://www.cnblogs.com/friday69/p/9389266.html
Copyright © 2011-2022 走看看