Mysql01

--給表添加註釋
comment on table emp is '僱員表';
--給列添加註釋
comment on column emp.ename is '僱員姓名';

/*sql語句學習
SELECT [DISTINCT] {*,column alias,..}
FROM table alias
Where 條件表達式
*/

--查詢僱員表中部門編號是10的員工
select empno,ename,job from emp where deptno = 10;
--dinstinct 去除重複數據
select distinct deptno from emp;
--去重也可以針對多個字段,多個字段值只要有一個不匹配就算是不同的記錄
select distinct deptno,sal from emp;


--在查詢的過程中可以給列添加別名,同時也可以給表添加別名
select e.empno 僱員編號,e.ename 僱員名稱,e.job 僱員工作 from emp e where e.deptno = 10;
--給列起別名可以加as,也可以不加,看你心情
select e.empno as 僱員編號,e.ename  as 僱員名稱,e.job as 僱員工作 from emp e where e.deptno = 10;
--給列起別名,如果別名中包含空格,那麼需要將別名整體用“”包含起來
select e.empno as "僱員 編號",e.ename  as "僱員 名稱",e.job as "僱員 工作" from emp e where e.deptno = 10;
--查詢表中的所有字段,可以使用*,但是在項目中千萬不要隨便使用*,容易被打死
select * from emp;


/*
=,!=,<>,<,>,<=,>=,any,some,all
is null,is not null
between x and y
in(list),not in(list)
exists(sub-query)
like  _ ,%,escape ‘\‘   _\% escape ‘\’
*/
-- =
select * from emp where deptno = 20;
--!=
select * from emp where deptno !=20;
--<> 不等於
select * from emp where deptno <> 20;
--<,
select sal from emp where sal <1500;
-->,
select sal from emp where sal >1500;
--<=,
select sal from emp where sal <=1500;
-->=,
select sal from emp where sal >=1500;
--any,取其中任意一個
select sal from emp where sal > any(1000,1500,3000);
--some,some跟any是同一個效果,只要大於其中某一個值都會成立
select sal from emp where sal > some(1000,1500,3000);
--all,大於所有的值纔會成立
select sal from emp where sal > all(1000,1500,3000);
--is null,在sql的語法中,null表示一個特殊的含義,null != null,不能使用=,!=判斷,需要使用is ,is not
select * from emp where comm is null;
--,is not null
select * from emp where comm is not null;
select * from emp where null is null;
--between x and y,包含x和y的值
select * from emp where sal between 1500 and 3000;
select * from emp where sal >=1500 and sal <=3000;
--需要進行某些值的等值判斷的時候可以使用in和not in
--in(list),
select * from emp where deptno in(10,20);
--可是用and 和or這樣的關鍵字,and相當於是與操作,or相當於是或操作
--and和or可能出現在同一個sql語句中,此時需要注意and和or的優先級
--and 的優先級要高於or,所以一定要將or的相關操作用()括起來,提高優先級
select * from emp where deptno = 10 or deptno = 20;
--not in(list)
select * from emp where deptno not in(10,20);
select * from emp where deptno != 10 and deptno !=20;
/*exists(sub-query),當exists中的子查詢語句能查到對應結果的時候,
意味着條件滿足
相當於雙層for循環
--現在要查詢部門編號爲10和20的員工,要求使用exists實現
*/
select * from emp where deptno = 10 or deptno = 20;
--通過外層循環來規範內層循環
select *
  from emp e
 where exists (select deptno
          from dept d
         where (d.deptno = 10 or d.deptno = 20)
           and e.deptno = d.deptno)
/*
模糊查詢:
like  _ ,%,escape ‘\‘   _\% escape ‘\’
在like的語句中,需要使用佔位符或者通配符
_,某個字符或者數字僅出現一次
%,任意字符出現任意次數
escape,使用轉義字符,可以自己規定轉義字符
使用like的時候要慎重,因爲like的效率比較低
使用like可以參考使用索引,但是要求不能以%開頭
涉及到大文本的檢索的時候,可以使用某些框架 luence,solr,elastic search
*/
--查詢名字以S開頭的用戶
select * from emp where ename like('S%')
--查詢名字以S開頭且倒數第二個字符爲T的用戶
select * from emp where ename like('S%T_');
select * from emp where ename like('S%T%');
--查詢名字中帶%的用戶
select * from emp where ename like('%\%%') escape('\')
/*
order by進行排序操作
默認情況下完成的是升序的操作,
asc:是默認的排序方式,表示升序
desc:降序的排序方式
排序是按照自然順序進行排序的
如果是數值,那麼按照從大到小
如果是字符串,那麼按照字典序排序
在進行排序的時候可以指定多個字段,而且多個字段可以使用不同的排序方式
每次在執行order by的時候相當於是做了全排序,思考全排序的效率
會比較耗費系統的資源,因此選擇在業務不太繁忙的時候進行
*/
select * from emp order by sal;
select * from emp order by sal desc;
select * from emp order by ename;
select * from emp order by sal desc,ename asc;
--使用計算字段
--字符串連接符
select 'my name is '||ename name from emp;
select concat('my name is ',ename) from emp;
--計算所有員工的年薪
select ename,(e.sal+e.comm)*12 from emp e;
--null是比較特殊的存在,null做任何運算都還是爲null,因此要將空進行轉換
--引入函數nvl,nvl(arg1,arg2),如果arg1是空,那麼返回arg2,如果不是空,則返回原來的值
select ename,(e.sal+nvl(e.comm,0))*12  from emp e;
--dual是oracle數據庫中的一張虛擬表,沒有實際的數據,可以用來做測試
select 100+null from dual;
--A
select * from emp where deptno =30;
--B
select * from emp where sal >1000; 
--並集,將兩個集合中的所有數據都進行顯示,但是不包含重複的數據
select * from emp where deptno =30 union
select * from emp where sal >1000;
--全集,將兩個集合的數據全部顯示,不會完成去重的操作
select * from emp where deptno =30 union all
select * from emp where sal >1000;
--交集,兩個集合中交叉的數據集,只顯示一次
select * from emp where deptno =30 intersect 
select * from emp where sal >1000;
--差集,包含在A集合而不包含在B集合中的數據,跟A和B的集合順序相關
select * from emp where deptno =30 minus 
select * from emp where sal >1000;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章