常用语句查询
## 模糊查詢,_ 匹配一個字符,% 匹配多個字符
-- 下面的意思是:查詢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
總結
 
                            
                        - 上一篇: 【历史上的今天】7 月 3 日:第一篇介
- 下一篇: 人工智能还不错,人工智障就算了
