DQL:查询语言
DML:数据操作
DDL:数据定义
- dual表
是系统表,因为数据库语句必须完整,实际没有意义
select 1+2 from dual - 别名
select user_id “id”,user_name name,user_phone as “phone” from user_tab; - 链接
select t.name ||‘的电话是’||t.phone from user_tab “t”; - 匹配
模糊匹配%or_(一位)
id like _a%(第二位为a) - 去重
select distinct job from emp; - and和or,in(),not()
and优先于or
where name in (‘张三’,’李四’);
where not(name like ‘张%’); - 排序
asc 升序(默认)
desc 降序
order by name desc,user_id “id” asc; - rownum
1.只支持<和<=(=1和>=1的意思是1,额外支持)
2.必须从第一列开始
exp:
显示后十条:
select * from (select * from emp order by rownum desc) where rownum <11;
显示5-10条:
select * from (select rownum r,name from emp where rownum <11) “s” where s.r >5 ; - 集合函数
不可用于查询条件,除了count(),只能用having
count(),max(),min(),avg(),sum(),sum()/count() - 字符函数
upper()大写
lower()小写
initcap()首字母大写
sbustr(‘ABCdef’,4,6) def
sbustr(‘ABCdef’,1,1) A
sbustr(‘ABCdef’,0,1) A
sbustr(‘ABCdef’,-3,2) de - 数字函数
trunc(1234.56) from dual 1234
round(1234.56) from dual 1235
select trunc(123.45,1)from dual 123.4
trunc(123.45,-1) 120
trunc(123.45,-3) 0 - 日期
to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’)(把时间改为字符串)
to_date(‘1990-01-23’,’yyyy-mm-dd’)(把字符串转为时间) - 子查询
一般不在子查询中用order by
多行子查询比较符:in(等于任意一个)\any(任意一个比较,大于最小)\all(大于最大) - 多表查询
笛卡尔积:
select * from score s1,course c,student s2 where s1.sid=s2.sid, and s1.cid=c.cid;
内连接:
select * from score s1 inner join course c on s1.cid=c.cid inner join student s2 on s1.sid=s2.sid;
左外连接(左表全部显示):
select * from score s1 left outer join course c on s1.cid=c.cid left outer join student s2 on s1.sid=s2.sid;
右外连接:rightrt outer join on
全连接:full outer join on - 分组查询:
select (中间只能出现被by的字段和组函数(max(),min()等)) from
group br 放在 where 后面
select dept_id,avg(salary) from s_emp group by dept_id having avg(salary)>(select avg(salary) from s_emp where dept_id = 21) ;