zoukankan      html  css  js  c++  java
  • mysql单表查询

    单表查询

    基础数据查询(重命名,去重,四则运算,拼接)

    • select * from 表名;
      • 查看所有表中所有数据
      • select * from t1;
    • select 字段名 [as] 新的字段名 from 表名;
      • 临时重命名字段名
      • select username name,post_comment as p_desc from t1;
    • select distinct 字段名 from 表名;
      • 对查询的数据去重
      • select distinct id,username from t1;
    • 对查询到的数据进行四则运算(加减乘除)
      • select username,salary*12 ann_salary from t1;
    • select concat( 拼接内容) from 表名;
      • 对查询到的数据进行拼接
      • select concat(id,' : ',name) from t1;
    • select concat_ws( '拼接内容',字段名1,字段名2,….) from 表名;
      • 对查询到的数据进行按拼接内容分隔拼接
      • select concat_ws( ' : ',id,username,salary) from t1;

    筛选关键字where

    where用于筛选所有符合条件的行

    • 比较运算
      • < > <+ >= <> !=
        • 其中<>等同于!=:不等于
    • 范围
      • between 值1 and 值2:范围为[值1,值2]
        • select * from t1 where age between 18 and 40;
      • in (值1,值2,值3,值4 ):范围为值1或值2或值3或值4
        • select * from t1 where age in (10,20,30,40);
    • 模糊匹配
      • like:模糊匹配like后面接字符
        • %:表示任意字段的任意字符
        • select * from t1 where name like '邓%';
        • __:表示一个字段的任意字符
        • select * from t1 where name like '邓__ __';
      • regexp:正则匹配
        • '^a'
        • 'b$'
    • 逻辑运算
      • not
      • and
      • or
    • 查看数据为null的情况
      • is
      • select * from t1 where post_comment is not null;

    分组聚合

    分组

    • group by
      • select post from employee group by post;

    聚合函数

    • count( ):求个数
    • max( ):求最大值
    • min( ):求最小值
    • sum( ):求总和
    • avg( ):求平均值
      • select post,count(id) from employee group by post;

    having

    • 筛选符合条件的组,放在分组group后面,在where中不能使用
    • 一定要先分组再使用having筛选
      • select post,count(id) from employee group by post having count(id)>4;

    order by

    • 查询排序,将所查询到的数据按照一定条件进行排序
    • 默认是升序(从小到大)排序 :asc
    • 降序(从大到小)排序:desc
    • order by age,salary; 优先按照age进行从小到大排序,然后在age相同时按salary从大到小排序
      • select * from employee order by age,salary desc;

    limit

    • 限制查询
    • limit m,n /limit n offset m
      • 从m+1项开始取n项,如果m不写,默认为0,如limit 1,2表示从第2项开始取后面两项
      • select * from employee order by age limit 2
      • select * from employee order by age limit 2,1

    查询数据关键字的优先级

    关键字 描述
    1 from 从xx表查询
    2 where 筛选符合条件的行
    3 group by 分组
    4 select 查询选择(与having优先级差不多)
    5 having 筛选符合条件的组(与select优先级差不多)
    6 order by 查询排序
    7 limit 限制查询
  • 相关阅读:
    commands.getstatusoutput和subprocess.call结果不一致
    win10 企业版 2015 长期服务激活
    pycharm设置护眼模式
    实战--滚动菜单
    javascript中this的用法
    jQuery之remove与empty的区别
    clone方法案例实践
    jQuery内部插入与外部插入
    jQuery文档处理
    2020/02/11星期二复习
  • 原文地址:https://www.cnblogs.com/Programmatic-yuan/p/13037739.html
Copyright © 2011-2022 走看看