1. 程式人生 > 其它 >基本SQL與實戰 轉至元資料結尾

基本SQL與實戰 轉至元資料結尾

1. 基本SQL語句

  • 選擇:select * from table1 where 範圍
  • 插入:insert into table1(field1,field2) values(value1,value2)
  • 刪除:delete from table1 where 範圍
  • 更新:update table1 set field1=value1 where 範圍
  • 查詢:select * from table1 where field1 like '%value1%' ---like的語法很精妙,查資料!
  • 排序:select * from table1 order by field1,field2 [desc]
  • 總數:select count as totalcount from table1
  • 求和:select sum(field1) as sumvalue from table1
  • 平均:select avg(field1) as avgvalue from table1
  • 最大:select max(field1) as maxvalue from table1
  • 最小:select min(field1) as minvalue from table1

2. 基本SQL語句例項

1.簡單查詢(select,from)

語法:

select 函式; (檢視系統函式的方法"? functions")

select 欄位1,欄位2,...

from 表名

應用案例:

1.查詢當前登入使用者

select user();

2.檢視當前資料庫

select database();

3.檢視當前時間;

select now();

4.查詢部門表中所有資料?

select * from departments

;其中"*"代表所有欄位

5.查詢所有部門id,名字?

select department_id,department_namefrom departments

或者select department_id as id,department_namefrom departments d;

其中"id"為列別名,"d"為表別名,中間的"as" 可以省略

6.查詢僱員薪水加300以後的值?

select first_name,salary+300 newSalaryfrom employees;

--------------------------------------- ------------------

2. 限制查詢(limit,where)

1.查詢所有僱員的名字,薪水,只顯示前5條記錄。

select first_name,salary

from employees

limit 5;

2.查詢所有僱員的名字,薪水, 只顯示第3條到第5條資料。

select first_name,salary

from employees

limit 2,3; (從>2位置取3條)

limit子句通常應用於分頁查詢現有一需求:10條記錄,每頁最多顯示3條,總計分多少頁?4頁公式: 10%3==0?10/3:10/3+1

如何取出第二頁的資料?(以僱員為例)select first_name,salaryfrom employeeslimit 3,3;

公式: limit (頁碼-1)*3,3;(其中3為每頁最多顯示的記錄數)

說明:假如一個查詢語句中有limit子句,那麼此子句要放在所有語句的最後。

limit 子句的另外一種形式: select first_name,salary from employees limit 3 offset 2; (從>2位置取3條)

3.查詢薪水大於10000的僱員的名字和薪水

select first_name,salary

from employees

where salary>10000;

其中"where"子句後是查詢條件,假如有where子句,where子句一定是跟在 from子句後。

4.查詢薪水在10000到20000之間的僱員 的名字和薪水

select first_name,salary

from employees

where salary>=10000 && salary<=20000;

5.查詢薪水在10000到20000之間的僱員的名字和薪水,只顯示前兩條記錄。

select first_name,salary

from employees

where salary>=10000 && salary<=20000

limit 2;

6.查詢1999年入職的所有員工的名字,薪水。

方法1:

select first_name,salary,hire_date

from employees

where hire_date like '1999%'

其中"%" 代表任意的0個或多個字元。

方法2:

select first_name,salary,hire_date

from employees

where year(hire_date)='1999'

7.查詢所有2月份入職的員工的名字,薪水?具體入職日期?

select first_name,salary,hire_date

from employees

where month(hire_date)='02';

或者

select first_name,salary,hire_date

from employees

where hire_date like '%-02-%'

8.查詢所有佣金為null僱員的名字,薪水?

select first_name,salary

from employees

where commission_pct is null

9.查詢名字中第二個字母為a的僱員的名字?

select first_name

from employees

where first_name like '_a%';

其中"_"表示任意的1個字元;

----------------------------------------- --------------------

3. 排序查詢(order by 列名 [ASC|DESC])

1.查詢所有僱員資訊,按入職日期升序排序。

select first_name,hire_date

from employees

order by hire_date asc;

說明:假如是升序asc可以省略。

2.查詢所有薪水大於8000的僱員的名字和薪水,並按薪水降序排序。

select first_name,salary

from employees

where salary>8000

order by salary desc;

假如在此查詢語句中沒有limit子句,

那麼order子句就是最後。

3.查詢所有薪水大於8000的僱員的名字和薪水-並按薪水降序排序,薪水相同按入職日期,升序排序。

select first_name,salary sal,hire_date

from employees

where salary>8000

order by sal desc,3 asc;

4.查詢所有僱員中薪水最高的僱員的名字和薪水

select first_name,salary,hire_date

from employees

order by salary desc

limit 1;

------------------------------------------ -----------------

4.分組查詢(group by ,having )

1.求每個部門的平均薪水?

select department_id,avg(salary)

from employees

where department_id is not null

group by department_id

其中"group by"子句用於分組。

2.求每個部門的平均薪水,只顯示平均薪水

大於5000的記錄。

select department_id,avg(salary)

from employees

where department_id is not null

group by department_id

having avg(salary)>5000;

假如有having子句,一定要跟在group by

子句之後。用於對分組以後的結果進行限制。

3.求每個工種(job_id)的平均薪水,並平均

薪水降序排序。

select job_id,avg(salary)

from employees

group by job_id

order by 2 desc;

4.求每年入職的人數?(按年份進行分組)

提示:人數統計用"count(*)";

select year(hire_date),count(*)

from employees

group by year(hire_date);

5.求每個部門的薪水的總和,

薪水的最大值,最小值?

select sum(salary),max(salary),min(salary)

from employees

where department_id is not null

group by department_id;

6.求僱員的總數,薪水的總和

select count(*),sum(salary),avg(salary)

from employees;

----------------------------------------- --------------------

5. 巢狀查詢(查詢中還有查詢)

1.查詢比僱員102的薪水還要高的僱員的

名字和薪水?

select first_name,salary

from employees

where salary>(

select salary

from employees

where employee_id=102);

2.求比部門80平均薪水高的部門的id和平均薪水。

select department_id,avg(salary)

from employees

where department_id is not null

group by department_id

having avg(salary)>(

select avg(salary)

from employees

where department_id=80);

3.求人數最多的那個部門的部門名稱。

1)求每個部門的人數

2)求部門人數的最大值

3)求人數等於這個最大值的部門id

4)根據部門id求得部門名稱

select department_name

from departments

where department_id=(

select department_id

from employees

where department_id is not null

group by department_id

having count(*)=(

select max(cmax)

from (

select department_id,count(*) cmax

from employees

where department_id is not null

group by department_id) e1));

說明:"from"子句後有子查詢,子查詢後

一定要有一個別名(表別名)。

----------------------------------------- ---------------------

6. 多表查詢(inner join,outer join)

1.求僱員102所在的部門的部門名稱?

巢狀查詢:

select department_name

from departments

where department_id=(

select department_id

from employees

where employee_id=102);

多表查詢:

select employee_id,department_name

from employees e,departments d

where e.department_id=d.department_id

and e.employee_id=102;

或者

select employee_id,department_name

from employees e inner join departments d

on e.department_id=d.department_id

where e.employee_id=102;

其中"inner join" 就是內連線,"inner"可以

省略,"on"後寫連線條件,where 後寫查詢

條件。

2.求僱員102所在的部門的部門名稱以及這個部門所在的城市。

select employee_id,e.department_id,department_name,city

from employees e,departments d,locations l

where e.department_id=d.department_id

and d.location_id=l.location_id

and e.employee_id=102;

當select子句後出現了from後的表中

的一個公共欄位,此欄位要進行標識,

要麼使用表名,要麼使用表別名。

或者

select employee_id,department_name

from employees e join departments d

on e.department_id=d.department_id

join locations l

on d.location_id=l.location_id

where e.employee_id=102;

3.求每個僱員及這個僱員的部門資訊?

select first_name,department_name

from employees e left outer join departments d

on e.department_id=d.department_id;

其中"left outer join"為左外連線,

其含義為:左邊表滿足記錄的和不滿足

記錄的都顯示,右邊表只顯示滿足記錄的。

"outer"可以省略

  1. 求每個部門及這個部門中的僱員的資訊?

select first_name,department_name

from employees e right outer join departments d

on e.department_id=d.department_id;

其中"right outer join"為右外連線,

其含義為:右邊表滿足記錄的和不滿足

記錄的都顯示,左邊表只顯示滿足記錄的。

5.笛卡爾積

多張表查詢時,沒寫連線條件。

select first_name,salary,department_name

from employees,departments;

通常藉助此種形式的查詢返回的結果,做為

測試資料用。

例如:快速建表

create table temp

as

select first_name,salary,department_name

from employees,departments

========================================================