一、单表简单查询:
1、
select * from scott.emp
2、去重:
--去除重复记录 select distinct ssex from java0322; select distinct (ssex) from java0322;
3、别名:
--给指定字段指定别名 select ssid as 编号 ,sname as 姓名 from java
4、排序:
--排序 倒序 desc select * from java0322 order by ssid desc;
5、模糊查询:
--模糊查询 select * from java0322 where sname like '___'; select * from java0322 where sname like '%四%';
二、多表连接查询:
1、交叉连接:若查询共有字段,需要制定该字段来自哪个表格;—–自然连接
--内连接 select * from java0322 join sclass on java0322.cid =sclass.cid; -- 给表起别名 select * from java0322 a join sclass b on a.cid = b.cid;
2、外连接:
--左外连接(左边的全部) select * from sclass a left join java0322 b on a.cid =b.cid; --右外连接(右表的全部) select * from java0322 a right join sclass b on a.cid =b.cid; --全外连接 select * from sclass a full join java0322 b on a.cid = b.cid;
三、分组聚合:
1、group by:
-- 查询男女各多少(按性别分组后查询所有,然后再查个数) select ssex,count(*) from java0322 group by ssex;
2、
-- 查询性别大于1 的性别 select ssex from java0322 group by ssex having count(*)>1;
where 放在group 之前,分组之后条件用having;
四、子查询:
-- 子查询--查询性别为女的学生所在的班级 select cname from sclass where cid in (select cid from java0322 where ssex='女'); --查询阳光班的所有男同学的地址 select saddress from java0322 where ssex ='男'and cid in(select cid from sclass where cname='阳光班'); select saddress from java0322 a join sclass b on a.cid = b.cid where ssex='男' and cname='阳光班'; select * from (select sclass.cid,cname,ssid,sname from sclass join java0322 on java0322.cid = sclass.cid)
1、分页:
--分行---查询第2条 select * from(select rownum as num,sclass.cid,cname,ssid,sname from sclass join java0322 on java0322.cid = sclass.cid) where num >1 and num<3;