码迷,mamicode.com
首页 > 数据库 > 详细

MySQL中数据的基本查询方式

时间:2018-11-17 14:26:29      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:having   聚合函数   size   筛选   类型   关键字   查询方式   多少   情况下   

1.查询所有列

  select * from 表名称;

2.查询指定列

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

3.查询时添加常量列(临时备注)

  select 字段名,字段名,字段名,字段名 as 备注 from 表名称;

4.查询时合并列(合并列只能合并数值类型的字段)

  select 字段名,(字段名+字段名) from 表名称;

5.查询时去除重复记录

  select distinct 字段名 from 表名称;

6.条件查询(where)

  (1)逻辑条件:and(并)  or(或)

  select * from 表名称 where 字段名=值 and 字段名=值;

  select * from 表名称 where 字段名=值 or 字段名=值;

  (2)比较条件:>  <  >=  <=  =  <>(不等于)  between and(等价于>=且<=)

  (3)判空条件(null空字符串):is null  /  is not null  /  =  ‘  ‘  /<>  ‘  ‘

  null:表示没有值   /   空字符串:有值,但是值是空字符串

  判断null

  select * from 表名称 where 字段名 is null;

  判断空字符串

  select * from 表名称 where 字段名=‘  ‘;

  判断null和空字符串

  select * from 表名称 where 字段名 is null or 字段名=‘  ‘;

  查询不包括null和空字符串的字段

  select * from 表名称 where 字段名 is not null and 字段名<> ‘  ‘;

  (4)模糊条件:like

  通常用一下替换标记:

  %:表示任意个字符

  _:表示一个字符

  select * from 表名称 where 字段名 like ‘部分值%‘;

7.聚合查询(使用聚合函数的查询)

  常用的聚合函数:sum()求和   avg()求平均值   max()求最大值   min()求最小值   count()计数

  用法:select 聚合函数(字段名) from 表名称;

  注意:count()函数统计的数量不包含null的数据,使用count统计表的记录数,要使用不包含null值的字段。

8. 分页查询(limit起始行,查询几行)

  起始行从0开始

  分页:当前页  每页显示多少条

  分页查询当前页的数据的sql:select * from 表名称 limit(当前页-1) 每页显示多少条,每页显示多少条;

例如:查询第1,2条记录(第一页的数据)

  select * from 表名称 limit 0,2;(当前页-1再乘以2,显示几条数据)

  查询第3,4条记录(第二页的数据)

  select * from 表名称 limit 2,2;

  查询第5,6条记录(第三页的数据)

  select * from 表名称 limit 4,2;

  查询第7,8条记录

  select * from 表名称 limit 6,2; 

9. 查询排序(order by)

  语法:order by 字段 asc/desc

  asc:顺序,正序。数值:递增,字母:自然顺序(a-z)

  desc:倒序,反序。数值:递减,字母:自然反序(z-a)

  默认情况下,按照插入记录顺序排序

  select * from 表名称 order by 字段名 asc/desc;

  注意:多个排序条件

  select * from 表名称 order by 字段名 asc,字段名 desc;

10. 分组查询(group by)

  select 字段名(同一个) from 表名称 group by 字段名(同一个);

11. 分组查询后筛选

  注意:分组之前条件使用where关键字,分组之后条件使用having关键字,如分组后找出大于或者小于n的字段

  select 字段名,count(*) from 表名称 group by 字段名 having count(*) 比较条件 n;

MySQL中数据的基本查询方式

标签:having   聚合函数   size   筛选   类型   关键字   查询方式   多少   情况下   

原文地址:https://www.cnblogs.com/qq1312583369/p/9973612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!