1. 语法:
select 字段列表
from 表名
[where 查询条件]
[group by 分组]
[having 分组条件]
[order by 排序]
select * 代表查询所有的字段
select id as "编号",sname 学生姓名, age "[年龄]" --as 之后是别名 也可以直接省略
select t.*
from t_student t -- 给表取别名
where classid is null -- 空判断
where age not in (20,23,...) -- 范围判断
where age between 20 and 25 -- 区间判断 between'A' and 'Z'
where sname not like '% 江 %' --sname like '江 %' -- '江 %' 以江开头 '% 江' 以江结尾 '% 江 %' 包含江
-- 模糊查询 _一个下划线表示一个位置
order by age -- asc 升序 desc 降序 默认升序
select distinct age,sex from t_student --distinct 去掉重复记录
统计函数:
count: 统计条数
- select count(*) from t_student
- select count(id) from t_student
- select count(classid) from t_student
-- 统计的是该列中非空的记录的个数
- select count(1) from t_student;
- select id,sname,age,sex,classid,1,2,3 from t_student
sum: 求和
select sum(age) from t_student;
min: 取最小值
select min(age) from t_student;
max: 取最大值
select max(age) from t_student;
avg: 取平均值
- select avg(age) from t_student;
- select sum(age),min(age),max(age),avg(age) from t_student;
count(1)与 count(*)比较:
如果你的数据表没有主键, 那么 count(1)比 count(*)快
如果有主键的话, 那主键 (联合主键) 作为 count 的条件也比 count(*)要快
如果你的表只有一个字段的话那 count(*)就是最快的啦
count(*) count(1) 两者比较. 主要还是要 count(1)所相对应的数据字段.
如果 count(1)是聚索引, id, 那肯定是 count(1)快. 但是差的很小的.
因为 count(*), 自动会优化指定到那一个字段. 所以没必要去 count(?), 用 count(*),sql 会帮你完成优化的
count 详解:
count(*)将返回表格中所有存在的行的总数包括值为 null 的行, 然而 count(列名)将返回表格中除去 null 以外的所有行的总数(有默认值的列也会被计入).
distinct 列名, 得到的结果将是除去值为 null 和重复数据后的结果
group by : 分组函数
- select age,sex
- from t_student
- group by age,sex
-- 注意: 分组函数中的 字段列表只能出现分组的字段和统计函数
-- 分组函数在没有统计函数使用的时候作用和 distinct 是一样的
- select sex,count(sex)
- from t_student
- group by sex
-- 分组函数[聚合函数] 在没有和 group by 一块使用的时候统计的是查询的所有的数据
-- 如果和 group by 一块使用的化, 那么统计的是分组后的各组数据
- select classid,sex,count(1)
- from t_student
- group by classid,sex
having count(1)> 1 -- 分组后的条件
- select classid,sex,count(1)
- from t_student
where age> 20 -- 分组之前加条件
group by classid,sex
where 和 having 的区别
where 只能跟在 from 后面 表示对查询的数据源过滤
having 只能出现在 group by 后面, 对分组后的数据进行过滤,
常用函数:
concat: 连接函数
select concat(id,sname),length(sname) from t_student
日期函数:
字符串转 date: to_date
update t_student set birth=to_date('1990-01-01','yyyy-mm-dd')
date 转字符串: to_char
select sysdate
,to_char(sysdate,'yyyy-mm-dd hh:mi:ss')-- 在数据库中是 HH24 mi
- ,to_char(sysdate,'yyyy-mm-dd')
- ,to_char(sysdate,'yyyy-mm')
- ,to_char(sysdate,'yyyy')
- from dual;
months_between(sysdate,date);-- 两者时间的月份数
add_months: 在当前时间的基础上增加月份数
select add_months(sysdate,12) from dual;
last_day(): 返回指定日期的当月的最后一天
select last_day(sysdate) from dual;
extract: 截取日期指定部分的内容
select extract(DAY from sysdate) from dual; --dual 是一个系统自带的虚表
nvl(column,value); 如果查询的字段为 null, 就用默认值填充
select id,sname,sex,nvl(sex,'哈哈') from t_student
decode: 类似于 Java 中的 if 语句
- select
- id,sname,sex
,decode(sex,1,'男') -- if(sex == 1){男}
- ,decode(sex,1,'男',2,'女') -- if(){}else if(){}
- ,decode(sex,1,'男',2,'女','不详')--if(){}else if(){}else{}
- from t_student
rowid: 行 id, 数据存储的位置, 唯一
rownum: 行号, 系统自动维护的, 从 1 开始自增, 有 1 才有 2
select t.*,rownum from t_student t
查询出学生表中前 5 条的学生记录
select t.*,rownum from t_student t where rownum <=5
查询出学生表中第 5 条到第 10 条的记录
select t.*,rownum from t_student t where rownum>=5 and rownum <=10 --- 这是错误的写法
--- 分页查询的实现方式(在 Oracle 中要这么实现)
- select t1.*,rownum
- from
(select t.*,rownum num from t_student t) t1 -- 先查询所有的数据和行号
where t1.num>= 5 and t1.num <=10 -- 再从中选出想要的部分
- select t2.*,rownum
- from
(select t1.* ,rownum num from t_student t1 where rownum <=10) t2 -- 先取上限的数据及行号
where t2.num>=5 -- 再取下限的数据
case 的使用
查询出学生表中年龄在[20 岁以下] [21-25] [26 以上] 分别有多少人
- select t.*,
- case when age <= 20 then 1 else 0 end "21 以下"
- ,case when age>20 and age <26 then 1 else 0 end "21-25"
- ,case when age>= 26 then 1 else 0 end "26 以上"
- from t_student t
- select
- sum(case when age <= 20 then 1 else 0 end ) "21 以下"
- ,sum(case when age>20 and age <26 then 1 else 0 end) "21-25"
- ,sum(case when age>= 26 then 1 else 0 end )"26 以上"
- from t_student t
- select
- count(case when age <= 20 then 1 else null end ) "21 以下"
- ,count(case when age>20 and age <26 then 1 else null end) "21-25"
- ,count(case when age>= 26 then 1 else null end )"26 以上"
- from t_student t
来源: http://www.bubuko.com/infodetail-2712274.html