SQL基础-数据库查询语言

基本的查询语法

select 列名 ---查询指定要显示的字段
from 表 ---选择要查询的表
where 限制条件 ---各种过滤条件
group by --按照指定列名进行分组统计
having -- 对分组后的结果进行限制
order by --对查询结果进行排序 默认升序 asc,降序用desc
limit N --给出N条数据

  • distinct 去除重复记录
select distinct 字段名...
from 表名
  • where 查询条件

比较:=, < ,<=, >, >=, <>
指定范围: between and 是两个边缘数值都包括的
集合: in ,not in
空值判断:not null, null
and 并列 同时满足条件
or 或 满足其中一个条件
like 模糊查询 %: 表示任意长度的字符串 ;_ :一个下划线表示一个字符

-- 以% 模糊查询
select *
from hy_order_20170410
where id like '%5';
-- 以_ 模糊查询
select *
from hy_order_20170410
where id like 'A0_5';
  • group by 对相关字段进行统计,group by 常跟着汇总函数一起使用

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

select memeber_id, sum(order_money)  as sum_money
from hy_order_20170410
group by memeber_id;
  • having 一定是与group by 一起使用,对汇总的结果进行过滤
select memeber_id, sum(order_money)  as sum_money
from hy_order_20170410
having sum(order_money)>100
group by memeber_id;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容