單表查詢,多表查詢,子查詢


一、單表查詢

1、完整語法(語法級別關機子的排列順序如下)

select distinct 字段1,字段2,字段3…… from 庫名.表名
Where 約束條件
Group by 分組依據
Having 過濾條件
Order by 排序的字段
Limit 限制顯示的條數
;
必須要有的關子字如下:
Select * from t1;
關鍵字執行的優先級
From
Where
Group by
Having
Distinct
Order by
Limit

2、簡單查詢

 

Select * from t1;
Select id,name,sex from t1;
Select distinct port from emp;
Select name,salary*12 as annual_salary from emp; #salary*12 as annual_salary 重命名

Select concat(“名字:”,name) as new_name,concat(“年齡:”,age) as new_age from emp;
Select concat(name,”:”,age) fron emp;#拼接

Select concat(name,”:”,age,”:”,sex) from emp;
Select concat_ws(“:”,name,age,sex)as info from emp;#多個內容拼接,符號相同的放在最前面,注意用concat_ws

 

一列內容分不同的需求滿足

 

 

Select
(
Case
When name=”egon’ then
Name
When name = “alex” then
Concat(name,”_bigsb”)
Else
Concat(name,”sb”)
End
) as new_name
From
Emp;

 

3、Where

Select * from emp where id >= 10 and id <=15; #等同於select * from emp where id between 10 and 15;
Select * from emp where id=6 or id=9 or id=12;#等同於select* from emp where id in(6,9,12);
_代表任意單個字符
%代表任意無窮個字符
Select * from emp where name like”__”;
Select * from emp where name like “jin%”;
Select * fromemp where id not in (6,9,12);
Select * from emp where id not between 10 and 15;

4group by 分組

 

什么分組:按照所有記錄相同的部分進行歸類,一定區分度低的字段

 

為何要分組:當我們要以組為單位進行統計時就必須分組,分組的目的是為了以組為單位進行統計的,再去考慮單條記錄毫無意義

 

 

嚴格模式下用分組,分組當中數據不清掉
set global sql_mode=”strict_trans_tables,only_full_group_by”;
注意:分組之后,只能查到分組的字段以及組內多條記錄聚合的成果
Select * from emp group by post;

聚合函數
Max
Min
Avg
Sum
Count

Select post,count(id) from emp group by post;
Select post ,max(salary) from emp group by post;
Select post,avg(salary) from emp group by post;
Select sex,count(sex) from emp group by sex;

統計出每個部分年齡30以上的員工的平均薪資
Select post,avg(salary) from emp where age >= 30 group by post;
注意:分組是在where之后發生的
Mysql>select * from emp where max(salary) > 3000;#報錯案例
Error 1111(HY000):Invalid use of group function 

group_concat 
Select post,group_concat(name,”:”,age) from emp group by post;

5Having 過濾條件

 

Where是在分組之前的過濾,即在分組之前做了一次整體性的篩選
Having是在分組之后的過濾,即在分組之后專門針對聚合的結果進行進一步的塞選

Select post,avg(salary) from emp group by post having avg(salary) > 10000;
Select post,avg(salary) from emp group by post;

 

6order by排序

 

Select * from emp order by age asc;#默認asc升序-》從小到大
Select * from emp order by age desc; #desc降序-》從大到小

Select* from emp by age asc,salary desc;#先按照age升序排列,如果age相同則按照salary降序排

Select post,avg(salary) from emp group by post order by avg(salary);

 

7limit限制顯示的條件

 

Select * from emp limit 3;
薪資最高那個人的詳細信息
Select * from emp order by salary desc limit 1;#選擇工資降序排列第一位工資人員信息
分頁顯示
Select * from emp limit 0,5;#從0開始往后取5條
Select * from emp limit 5,5;#從5開始往后取5條

正則表達式
Select * from emp where name regexp “^jin.*(g|n)$”;

 

二、多表查詢

 

 

 

 

1、笛卡爾積
Select * from emp,dep;
表格不單單是對照准確的,以及不准確的ID對照也會在內
Select * from emp,dep where emp.dep_id=dep.id;
Select * from emp,dep where emp.dep_id = dep.id and dep.name = “技術”;

 

2、內連接:只取兩張表有對應關系的記錄
Select * from emp inner join dep on emp.dep_id = dep.id;
Select * from emp inner join dep on emp.dep_id = dep.id
Where dep.name = “技術”;
3、左連接:在內連接的基礎上保留左表沒有對應關系的記錄
Select * from emp left join dep on emp.dep_id = dep.id;
4、右連接:在內連接的基礎上保留右表沒有對應關系的記錄
Select * from emp right join dep on emp.dep_id = dep.id;
5、全連接: 在內連接的基礎上保留左、右面表沒有對應關系的記錄
Select * from emp left join dep on emp.dep_id = dep.id
Union
Select * from emp right join dep on emp.dep_id = dep.id;

三、子查詢

 

 

子查詢:就是將一個查詢語句的結果用括號括起來當做另外一個查詢語句的條件去用
Select * from emp where dep_id in(select id from dep where name = ”技術” or name=”人力資源”);

每個部門最新入職的員工
Select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1
Inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
On t1.post = t2.post
Where t1.hire_date = t2.max_date;

Select * from emp inner join dep on emp.dep_id = dep.id;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM