1. 程式人生 > 其它 >基礎查詢(一)

基礎查詢(一)

技術標籤:資料庫plsqlsqlthinkphpmongodb

表的梳理

部門表

deptno:部門編號

dname:部門名稱

loc –location 地理位置

員工表

empno:員工編號

ename:員工姓名

job:工種

mgr:上級

hiredate:入職時間

sal:薪水

comm:津貼

deptno:部門編號

薪水等級表

grade:等級

losal-Lowestsal:最低薪水

hisal-highestsal:最高薪水

DML(Data manipulation language)

資料庫操縱語言(增刪查改表中資料)

基礎查詢

1、查詢單個欄位

select

dept.dname

from dept;



select

dname

from demp;

注意:select查詢

from:從哪裡來 來自 特指表

2、查詢多個欄位

select 
dname, loc
from dept;

3、將查詢到的欄位取別名

select 
ename as 僱員姓名
from emp;

select 
ename 僱員姓名
from emp;

select 
ename 僱員姓名
from emp as e;

給列和表名都可以取別名,as和不加as效果效能相同

4、條件查詢例項

符號練習:#查詢薪水=5000的員工

1、select 
ename, sal, job
from emp 
where sal=5000;

數字區間練習:#查詢job是MANAGER且薪水大於2000小於2500

2、select 
ename, job, sal 
from emp 
where sal between 2000 and 2500 and job='MANAGER';

判空練習:#津貼為空且job是是manager且薪水大於2000且小於2500的員工

3、select 
ename, job, sal, comm 
from emp 
where comm is null and sal between 2000 and 2500 and job=’MANAGER’;

或or練習#津貼為空且job是manager且薪水大於2000且小於2500的員工 和job是salesman,comm=500 的員工

4、select
ename, job, sal,comm
from emp
where (comm is null and sal between 2000 and 2500 job='MANAGER') 
or (job='SALESMAN' and comm='500');

in 練習#津貼為空 且job是manager 且薪水大於2000小於2500的員工 和job是salesman comm=500的員工,且同時查出薪水等於800 1600 3000的員工資訊

5、select 
ename,job,sal, comm
from emp 
where (comm is null and sal between 2000 and 2500 and job='MANAGER')
or (job='SALESMAN' and comm=500) 
or sal in(800, 1600, 3000);

not 練習#查出薪水不等於800 和1600的員工

6、select 
ename,job,sal,comm
from emp 
where not (sal=800 or sal=1600);

#以j開頭 以s結尾的員工

7、select 
ename, job, sal
from emp 
where ename like 'J%S';

#檢視第二個字是O 一共5個字母

8、select
ename, job, sal 
from emp
where ename like '_O___';