Oracle數據庫之查詢

1、單表簡單查詢:java

一、spa

select * from scott.emp

二、去重:code

--去除重複記錄

select distinct  ssex from java0322;
select distinct  (ssex) from java0322;

三、別名:blog

--給指定字段指定別名
select ssid as 編號 ,sname as 姓名 from java

四、排序:排序

--排序 倒序 desc
select * from java0322 order by ssid desc;

五、模糊查詢:ci

--模糊查詢
select * from java0322 where sname like '___';
select * from java0322 where sname like '%四%';

2、多表鏈接查詢:class

一、交叉鏈接:若查詢共有字段,須要制定該字段來自哪一個表格;-----天然鏈接select

--內鏈接
select * from java0322 join sclass on java0322.cid =sclass.cid;
-- 給表起別名
select * from java0322 a join sclass b on a.cid = b.cid;

二、外鏈接:分頁

--左外鏈接(左邊的所有)
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;

3、分組聚合:查詢

一、group by:

-- 查詢男女各多少(按性別分組後查詢全部,而後再查個數)
select ssex,count(*) from java0322 group by ssex;

 二、

-- 查詢性別大於1 的性別
select ssex from java0322 group by ssex having count(*)>1;

where 放在group 以前,分組以後條件用having;

4、子查詢:

-- 子查詢--查詢性別爲女的學生所在的班級
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)

一、分頁:

--分行---查詢第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;
相關文章
相關標籤/搜索