1. 程式人生 > >常用語句查詢

常用語句查詢

-- # 模糊查詢,_ 匹配一個字元,% 匹配多個字元

-- # 下面的意思是:查詢name欄位第二個字元為s的所有項

-- select * from test where name like '_s%';

# 把欄位name 重複的內容去除

-- select DISTINCT name from test;

-- # 多個欄位的數學操作,必須是可以計算的型別,如整形,浮點型

-- select *,age+score from test;

-- # 給某個欄位空值取某個預設值,取別名

-- SELECT *,(IFNULL(age,0) + IFNULL(score,0)) as total FROM test;

-- # 排序,預設asc升序排序,desc為降序排序,可以設定多個自動

-- SELECT * FROM test ORDER BY age desc, id desc;

-- # 或者下面的寫法

-- SELECT * FROM test ORDER BY age, id desc;

-- # 聚合函式,count()不為NULL的所有值,max(),min(),sum(),avg()

-- SELECT count(age),count(score) FROM test;

-- # 分組函式group by類似歸類

-- # 下面是把每個部門的人通過group_concat(name)查詢出來

-- SELECT party,group_concat(name) FROM Test GROUP BY party

-- # 查詢每個部門的薪水和顯示他們的名字

-- SELECT sum(salary),party,group_concat(name) FROM test GROUP BY party;

-- # 在使用分組時,select 後面的欄位一般都出現在GROUP BY 的後面,

-- # 否則語句查詢起來就沒有多大意義

-- SELECT name,party FROM test GROUP BY party,name

-- # GROUP BY + 聚合函式

-- SELECT party,group_concat(salary),sum(salary),count(*) FROM test GROUP BY party;

-- # 查詢每個部門的名稱並且工資大於1500的人數

-- SELECT party,count(*) from test where salary > 1500 GROUP BY party

-- # GROUP BY + having (having 相當於 where ,只能配合group by 中使用

-- # 查詢工資總和大於等於5000的部門名稱

-- select party,group_concat(salary),sum(salary) as total from test group by party having total > 5000

-- # HAVING 和 where 的區別

-- 1.Having 在group by 分組後進行過濾

-- 2.where 是在分組前對資料進行過濾

-- 3.having 後面可以使用分組函式(統計函式)

-- 4.where 後面不可以使用分組函式

-- 5.where 是對分組前記錄的條件,如果某行記錄沒有滿足WHERE子句的條件,

-- 那麼這條記錄就不會參加分組,而having是對分組後的資料進行過濾

# 查詢工資大於2000,工資總和大於4000的部門名稱以及工資和, 按工資總和降序排序

-- select party,GROUP_CONCAT(salary),sum(salary) as total from test where salary > 2000 GROUP BY party HAVING total > 4000 ORDER BY total desc

-- # 書寫順序 select from where GROUP BY having order by limit

-- # 執行順序 from where GROUP BY having select order by limit

-- # limit 從哪一行開始查,總共要查幾行

-- # limit 引數1,引數2(引數1是從第幾條開始查,引數2是要查詢幾條)

-- # limit 角標從0開始

-- # 用在分頁的情況中:

-- # 如: int curPage = 1 (當前頁); int pageSize = 10; (每頁10條資料)

-- # 第一頁為:0,10; 第二頁為:10,10,第二頁為:20,10

-- # 寫成變數的形式:limit (curPage-1) * pageSize, pageSize

# 資料的完整性:

-- 1.實體完整性:表中的一行(一條記錄)代表一個實體; 作用:標識每一行資料不重複,行級約束

-- 1.主鍵約束:每個表要有一個主鍵

-- 2.唯一約束:指定列的資料不能重複,可以為空值;格式(create table tablename(欄位名1 資料型別 UNIQUE);

-- 3.自動增長列

-- 2.域完整性:單元格的約束

-- 1.資料型別

-- 2.not null

-- 3.default 預設值

-- 3.引用完整性:表與表之間的對應關係:

# 新增約束:名字為score_student,score表的外來鍵sid,參考的是student表中的欄位id

-- alter table score add constraint score_student FOREIGN KEY(sid) REFERENCES student(id);

-- alter table score add constraint score_course Foreign key(cid) REFERENCES course(id);

# 多表查詢:

# 合併結果集:union 和 union all 多個表查詢同時返回結果集,union 能去重複的資料; 要保證列數量,列型別相同

-- create table a(name varchar(20), score int);

-- create table b(name varchar(20), score int);

-- insert into a values('a', 10),('b',20),('c', 30);

-- insert into b values('a', 10),('b',20),('d', 30);

# union and union all 查詢

-- select * from a union select * from b;

-- select * from a union all select * from b;

# 笛卡爾現象:

# 假設有2個集合:A = {a,b}, B = {0,1,2}

# 則兩個集合的笛卡爾積為:{(a,0),(a,1),(a,2),(b,0),(b,1),(b,2)}

# 可以擴充套件到多個集合的情況

# 為了不出現笛卡爾現象,得到的資料正確:在查詢的時候把主鍵和外來鍵保持一致

# 原理:逐行掃描,相等的留下,不相等的全不要;

-- select * from student,score where student.id = score.sid;

# 連線查詢:內連線(等值連線,自然連線,非等值連線);外連線(左連線,右連線,多表連線)

# 內連線查詢(和select * from student,score where student.id = score.sid)的效果一樣,還可以加入where條件

# inner 可以省略

-- select * from student inner join score on student.id = score.sid where score > 80;

# 左連線就是把兩表滿足條件相同的資料查出來,如果左邊表當中有不相等的資料,也把左邊表當中的資料查出來;

# 同理右連線也是一樣;

# outer 可以省略

-- select * from student left OUTER join score on student.id = score.sid

# 多表查詢:student, score , course 3個表,查詢學生的所有科目的成績;

# 99連線法:

-- select student.id,student.name,score.score,course.name from student,score,course

-- where student.id = score.sid and score.cid = course.id;

# 內連線查詢:

-- select student.id,student.name,score.score,course.name from student

-- INNER JOIN score ON student.id = score.sid

-- INNER JOIN course ON score.cid = course.id

# 自然連線:2個表必須有相同的列名和相同的型別;

-- SELECT * from student NATural Join score;

# 子查詢:一個select 語句中包含另外一個完整的select語句,或2個以上的select,那麼就是子查詢、

# 子查詢出現的位置: where後,把select 查詢出的結果作為另外一個select的條件值;

# from 後,把查詢出的結果當作一個新表;

# 查詢與唐僧同一個部門的員工,記得子查詢要加一個括號

-- select * from emp where depton = (select depton from emp where ename = '唐僧')

# 查詢部門編號為20的所有員工薪水大於16000的員工;

-- select * from (select ename,salary,depton from emp where depton = 20) as temp where temp.salary > 16000;

# 查詢工資高於豬八戒的員工

-- select * from emp where salary > (select salary from emp where ename = '豬八戒')

# 查詢工作和薪水和豬八戒相同的員工

-- select ename,salary from emp where (job,salary) in (select job,salary from emp where ename ='豬八戒')

# 或者用下面的查詢,多表查詢:

-- select e.ename,e.salary from emp as e, (select job,salary from emp where ename ='豬八戒') as res where e.job = res.job and e.salary = res.salary;

# 查詢有2個員工的上級資訊, 下面的in 也可以換成 =

-- select * from emp where empno in (select mgr from emp group by mgr HAVING count(mgr) > 2);

# 自連線

# 求1003員工編號,姓名,經理編號和經理名稱姓名

select e1.empno,e1.ename,e2.empno,e2.ename from emp e1, emp e2 where e1.mgr = e2.empno and e1.empno = 1003