一、過濾我就不細說了,where語句大家都會用的麼。
1.標準條件查詢(字符串大小寫敏感,日期格式敏感)
select 列名 from 表名 where condiaction1 [ and/or condition2 ...]
eg:select * from emp where sal > 1000;
select * from emp where sal >2000 and deptno = 20;
注意:這裏需要注意參數的默認設置,可以這樣查詢:select * from v$nls_parameters;
NLS_DATE_FORMAT參數注意了啊,DD-MON-RR格式,日期格式爲 “日-月-年”;
可以自己更改:alter session set NLS_DATE_FORMAT='yyyy-mm-dd',就可以按照自己的日期查詢了。
select * from emp where hiredate='1981-11-17';
2.In (set) 等於值列表中的一個
select 列名 from 表名 where列名 in (條件1,條件2...) [ and/or condition2 ...]
eg:select * from emp where deptno in (10, 30,null);--查詢部門號爲10,或者30,或者爲空的員工
select * from emp where deptno not in (10,30);--查詢部門號不爲10,或者30的員工
注意:如果集合中含有null,查詢中不可以使用 not in ,但可以使用in
3.like模糊查詢,通配符:%,_
select 列名 from 表名 where 列名 like "通配符+條件+通配符" [ and/or condition2 ...]
(條件中存在通配符可以設置轉義字符: like "\通配符+條件+通配符" escape '\',其中'\' 可以爲任何字符)
eg:select * from emp where ename like 'S%';--查詢姓名首字母爲 'S'的員工
select * from emp where ename like '____';--查詢姓名爲四位數的員工
insert into emp(empno,ename,sal,deptno) values(1001,'abc_abc',1000,20);
select * from emp where ename like 'abc\_abc' escape '\';--先插入數據,其次查詢含有'_'線的員工。
4.betweencondication1 and condication2 (包括邊界)
select 列名 from 表名 where 列名between condication1and condication2
eg:select * from emp where sal between 1000 and 5000;--查詢薪水在爲1000到5000的員工,包含1000,5000。
二、排序: order by
1.升序排序 aesc
select * from emp order by sal; --select * from emp order by sal aesc; 相同的
select ename,sal from emp order by sal;
2.降序排序desc
小技巧:以爲上條語句爲:select * from emp order by sal;所以有一下語句
--a append 追加字符 直接輸入:
a desc;
效果爲:
然後輸入'/'回車查詢即可。
效果就是:select ename,sal from emp order by sal desc;
還有也可以使用序號去排序:
select ename,sal from emp order by 2 desc;--用序號代替sal
3.order by 後面+多列;先按照order by 後第一列排序,然後是第二列,以此類推
select * from emp order by sal,deptno;--結果按照sal升序,然後deptno升序排序
select * from emp order by sal,deptno desc;--結果按照sal升序,然後deptno降序排序(距離desc最近的)
select * from emp order by sal desc,deptno desc;--結果按照sal降序,然後deptno降序排序
4.當含有空值時。
select * from emp order by comm;--升序
select * from emp order by comm
desc;--降序
發現了吧,是不是應該把空值排到最後呢。這樣寫:
select * from emp order by comm desc nulls last;