mysql

2020-08-11DQL_查询

2020-08-19  本文已影响0人  智障猿

语法

select 字段列表 from 表名列表 where 条件列表 group by 分组字段 having 分组之后的条件 order by 排序 limit 分页限定

基础查询

  1. 多个字段的查询
    select 列名,列名 from 表名;
  2. 去除重复
    select distinct 列名 from 表名;
  3. 计算列
    select 列名+列名 from 表名;
    select 列名+ifnull(列名,0) from 表名;
  4. 起别名
    select 列名+列名 as 别名 from 表名;

条件查询

  1. >,<,<=,>=,=,<>
  2. BETWEEN...AND...
    select * from 表名 where 列名 between 20 and 90;
    select * from 表名 where 列名 >=20 and 列名<=90;
  3. LIKE
    模糊查询
    占位符:_:单个字符的占位符;%:任意多个字符的占位符
    select * from 表名 where 列名 LIKE “%马%”;
    select * from 表名 where 列名 LIKE “ 马__”
  4. IS NULL
    select * from 表名 where 列名 is NULL;
    select * from 表名 where 列名 is not NULL;
  5. and 或 &&
  6. or 或 ||
  7. not 或 !
  8. in
    select * from 表名 where 列名 in (20,60,80);
    select * from 表名 where 列名= 20 or 列名=60 or 列名=80;

排序查询

聚合查询

将一列数据作为一个整体,进行纵向的计算,结果为单行单列的一个值

函数 说明
count 计算个数(一般选择非空的列)
max 计算最大值
min 计算最小值
sum 计算和
avg 计算平均值

分组查询

分页查询

上一篇下一篇

猜你喜欢

热点阅读