1. 程式人生 > 其它 >sql學習--簡單查詢

sql學習--簡單查詢

技術標籤:資料庫資料庫mysqlsqloracle

sql學習

查詢

簡單查詢

查詢全部

mysql:

SELECT *From emp

oracle:

SELECT *From emp;

查詢特定列

mysql:

SELECT empno,ename,job from emp

oracle:

SELECT empno,ename,
job from emp

別名使用

mysql:

select  e.empno '員工編號',e.ename '員工姓名',e.job '員工職位',e.mgr '上級領導',
       e.hiredate '入職時間',e.sal '薪水',e.comm '獎金',e.deptno '所在部門'
       from emp e

oracle:

select  e.empno '員工編號',e.ename '員工姓名',e.job '員工職位',e.mgr '上級領導',
       e.hiredate '入職時間',e.sal '薪水',e.comm '獎金',e.deptno '所在部門'
from emp e

去重(使用關鍵字distinct)

只有當查詢出來的所有列全部相同的時候才會合併
mysql:

select  distinct  job from emp;

oracle

select  distinct  job from emp;

字串連線查詢

mysql:

select concat('員工編號是:',empno,'姓名是:',ename,'的工作是:',job)
    '員工姓名是:姓名是:的工作' from  emp;

oracle:

select '員工編號是'|| empno||'姓名是:'||ename||'的工作是:'||job from
emp;

查詢中的四則運算

乘法(剩餘的一樣)

mysql:

select ename,sal*12 from emp
select concat('姓名是:',ename,'年薪是:',sal*12) as '姓名是: 年薪是:  ' from emp;

oracle:

select ename,sal*12 from emp
select concat('姓名是:',ename,'年薪是:',sal*12) as '姓名是: 年薪是:  ' from emp;

限定查詢

查詢工資大於1500的所有僱員

mysql:

select  ename '姓名',e.sal '薪水' from emp e where e.sal>1500

oracle:

select  ename '姓名',e.sal '薪水' from emp e where e.sal>1500

非空和空的限制

is not 和 is null 如果是not則是取非

SELECT *from emp WHERE comm is not null;
SELECT *from emp WHERE not(comm is null);

mysql:

select *from  emp where comm is not null

oracle:

select *from  emp where comm is not null

多條件查詢

使用 and 進行連線

mysql:

 select  *from  emp where sal>1500 and comm is not null;

oracle:

 select  *from  emp where sal>1500 and comm is not null;

使用or進行連線

 select  *from  emp where sal>1500 orcomm is not null;

oracle:

 select  *from  emp where sal>1500 or comm is not null;

between and 的用法

下面的兩句sql在查詢結果上一模一樣

mysql:

SELECT *from emp WHERE sal>=1500 AND sal<=3000;
SELECT *from emp WHERE sal BETWEEN 1500 and 3000;

oracle:

SELECT *from emp WHERE sal>=1500 AND sal<=3000;
SELECT *from emp WHERE sal BETWEEN 1500 and 3000;

IN的用法

範例:查詢僱員編號是7369,7499,7521的僱員編號的具體資訊
下面的兩條sql在查詢結果完全相同
此外還可以使用 not in

mysql:

select *from  emp where empno='7369' or empno='7499' or empno='7521';

select *from emp where empno in('7369','7499','7521');
select *from emp where empno not in('7369','7499','7521');

oracle:

select *from  emp where empno='7369' or empno='7499' or empno='7521';

select *from emp where empno in('7369','7499','7521');
select *from emp where empno not in('7369','7499','7521');

萬用字元

在常用的站點中經常會有模糊查詢,即:輸入一個關鍵字,把符合的內容全部的查詢出來,在sql中使用LIKE語句完成。
在LIKE中主要使用以下兩種萬用字元
“%”:可以匹配任意長度的內容
“_”:可以匹配一個長度的內容
範例:查詢出所有僱員姓名中第二個字元包含“M”的僱員
mysql:

select *from emp where ename like '_M%';

oracle:

select *from emp where ename like '_M%';

在LIKE中如果沒有關鍵字表示查詢全部

對結果集排序

在sql中可以使用ORDER BY對查詢結果進行排序
語法:SELECT * |列名 FROM 表名 {WEHRE 查詢條件} ORDER BY 列名1 ASC|DESC,列名2…ASC|DESC
範例:查詢僱員的工資從低到高
分析:ORDER BY 列名 預設的排序規則是升序排列,可以不指定ASC,如果按著降序排列必須指定DESC
mysql:

# 升序
select *from emp order by sal asc;
# 降序
select *from emp order by sal desc;

oracle:

# 升序
select *from emp order by sal asc;
# 降序
select *from emp order by sal desc;

如果存在多個排序欄位可以用逗號分隔

select *from emp order by sal asc,hiredate desc ;

注意ORDER BY語句要放在sql的最後執行。