首页 技术 正文
技术 2022年11月21日
0 收藏 864 点赞 3,949 浏览 1512 个字
  • 排序 :order by  desc

      select * from <tablename> order by <字段名> desc;order by默认升序 desc 降序

  • 分组 :group by 按照字段进行分组

      select  <查询内容> from <tablename> group by <字段名> ;

  • 空值 and 非空值 :null ,not null 查询字段为空或者非空的记录

      select <查询内容> from <tablename> where <字段名> is null not null  ;

常用的聚合汇总函数

  • max and min :返回某列最大最小值

      select max(字段名) , min(字段名) from <tablename>;

  • count :返回某列记录数

      select count(*) from <tablename>;

  • sum :返回某列之和

      select sum(字段名) from <tablename>;

  • avg :返回某列平均值

      select avg(字段名) from <tablename>;

其他操作

  • and or : 在where子语句中将多个条件结合起来 and 优先级高于or

      select * from <tablename> where 条件1 and 条件2 or 条件3;

  • like : 在where子语句中,搜索匹配字段的指定内容,通常与%通配符连用

      select * from <tablename> where <字段名> like “%匹配内容%” ;

      Tips:匹配内容MySQL不区分大小写,若想严格区分大小写,利用binary关键字。

      select * from <tablename> where <字段名> like binary “%匹配内容%” ;

  • in not in :类似于python中的成员运算符,用于查找在范围内的记录

      select * from <tablename> where <字段名> in not in(” 属性值1 “,” 属性值2 “) ;

  • date_format : 按照指定日期格式输出

      select date_format(birthdate,’%Y-%m’) from <tablename> ; 日期按照年-月输出

  • distinct :去除重复值

      select count(distinct 字段名) from <tablename>;

  • between :where子语句后,规定某字段查询区间

      select * from <tablename> where <字段名> between ‘ 范围1’ and ‘范围2 ‘;

  • having :与where类似,同为条件筛选语句。

      select count(*) from <tablename> where ‘条件1’ group by <字段名> having ‘条件2’ ;

      having 与 where区别:执行优先级 where > 聚合函数(count sum max)>having

                 where子句是在分组之前过滤数据,条件中不能包含聚合函数。

                 having子句是对分组之后过滤数据,条件中经常包含聚合函数。

  • union:多个查询结果做并集

      select <字段名> from <tablename>

      union

      select <字段名> from <tablename>

MySQL条件分支

  CASE column

    WHEN 条件1 THEN 表达式1

    WHEN 条件2 THEN 表达式2

    …

  ELSE 表达式

  END AS column_alias ;

2020-03-12 15:35

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892