MySQL--詳細查詢操作(單表記錄查詢、多表記錄查詢(連表查詢)、子查詢)


一.單表查詢

1、完整的語法順序( 可以不寫完整,其次順序要對 )
(不分組,且當前表使用聚合函數: 當前表為一組,顯示統計結果 )
select distinct*,查詢字段1,查詢字段2,表達式, 聚合函數。。】 from 表名      ##############distinct 去重, 與查詢字段一個級別
    where 分組之前的過濾條件
    group by 分組依據                           ##############可以分多次  例如 group by age,sex  每個年齡階段的男女
    having 分組之后的過濾條件
    order by 排序字段                           ###############可以排多次 例如  order by a,b desc a默認從小到大,只指定了b desc 從大到小
    limit start,n  或者 n;             ###############開始條數,查詢n條     或者   查詢出來的數據顯示n條

 

例子:
select distinct age,id from user where age>18 group by id having age>24 order by age desc limit 3;   
依次:1. where age>18  2. group by id  3.having age>24  4.查詢(先去重后的age字段,id字段)  distinct age ,id
     
        5. order by age desc  6.limit 3

 

2、執行順序
def from(dir,file):
    open('%s\%s' %(dir,file),'r')
    return f
def where(f,pattern):
    for line in f:
        if pattern:
            yield line
def group_by():
    pass
def having():
    pass
def distinct():
    pass
def order_by():
    pass
def limit():
    pass
def select():
    res1=from()   
 #在硬盤中找到表
    res2=where(res1,pattern)  
 #拿着where指定的約束條件,去文件/表中取出一條條記錄,在內存中得到一張虛擬的表, 如果沒有where,默認全True
    res3=group_by(res2,)  
 #將取出的一條條記錄進行分組group by,如果沒有group by,默認整體作為一組
    res4=having(res3)   
 #將分組的結果進行having過濾,如果沒有having,默認全True
    res5=distinct(res4)  
 #去重, 如果沒有distinct,默認不去重
    res6=order_by(res5)  
 #將結果按條件排序
    limit(res6)    
 #限制結果的顯示條數

 

3、按照優先級的級別寫SQL語句
a、先確定是哪張表 from db39.emp
b、是否有過濾條件 where name like '%i%'
。。。
z、放功能 select

 

4、where過濾
where字句中可以使用:
1. 比較運算符:> < >= <= <> !=  #不等於用 != 不用 <>
   select id,name from db39.emp where id >= 3 and id <= 6
2. between 80 and 100
   select *  from db39.emp where id between 3 and 6;  # >=3 and <=6
3. in(80,90,100) 值是80或90或100
   select * from emp where salary in (20000,18000,17000); # select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
4. like 'egon%', pattern可以是%或_, %表示任意多字符, _表示一個字符
   select name,salary from db39.emp where name like '%i%'  #要求:查詢員工姓名中包含i字母的員工姓名與其薪資
   select name,salary from db39.emp where name like '____';  #要求:查詢員工姓名是由四個字符組成的的員工姓名與其薪資
   select name,salary from db39.emp where char_length(name) = 4;   #結果與上一條一致
5. 邏輯運算符:在多個條件直接可以使用邏輯運算符 and or not
   select *  from db39.emp where id not between 3 and 6;
   select * from emp where salary not in (20000,18000,17000);
   要求:查詢崗位描述為空的員工名與崗位名
   select name,post from db39.emp where post_comment is NULL;  #針對NULL必須用is,不能用=
   select name,post from db39.emp where post_comment is not NULL;
   #NULL指的是不占任何存儲空間,在mysql中空字符串也是占存儲空間的,即不為空(NULL

 

5、group by分組
 如果不設置成only_full_group_by模式,分完組后用*默認取出的是組內的第一個人的數據。但分完組后單獨取組內的某個元素是沒有意義的,所以,分組前,一般會對模式做如下處理
    #設置sql_mode為only_full_group_by,意味着以后但凡分組,只能取到分組的依據
    mysql> set global sql_mode="strict_trans_tables,only_full_group_by";
    #聚合函數 group function(一般與分組連用)
    select post,max(salary) from emp group by post; #取不出組內的元素name, age..,只能取組名(分組依據)或用聚合函數
    select post,min(salary) from emp group by post;
    select post,avg(salary) from emp group by post;
    select post,sum(salary) from emp group by post;
    select post,count(id) from emp group by post;
    #group_concat(分組之后用):把想要用的信息取出;字符串拼接操作
    select post,group_concat(name) from emp group by post;
    select post,group_concat(name,"_SB") from emp group by post;
    select post,group_concat(name,": ",salary) from emp group by post;
    select post,group_concat(salary) from emp group by post;
    # 補充concat(不分組時用):字符串拼接操作
    select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪資 from emp;
    # 補充as語法:為字段或表取別名
    select name as 姓名,salary as 薪資 from emp;  # as可省略
    mysql> select emp.id,emp.name from emp as t1; # 報錯 emp先變成了t1
    mysql> select t1.id,t1.name from emp as t1;  # 同 mysql> select id,name from emp as t1;
    # 查詢四則運算
    select name,salary*12 as annual_salary from emp;
    #分組練習
    select post,group_concat(name) from emp group by post;  #查詢崗位名以及崗位包含的所有員工名字
    select post,count(id) from emp group by post; #查詢崗位名以及各崗位內包含的員工個數
    select sex,count(id) from emp group by sex;  #查詢公司內男員工和女員工的個數
    select post,avg(salary) from emp group by post;  #查詢崗位名以及各崗位的平均薪資
    select sex,avg(salary) from emp group by sex;  #查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資
    select post,avg(salary) from emp where age >= 30 group by post; #統計各部門年齡在30歲以上的員工平均工資

 

6、having過濾 (一定要用 組名(分組依據)聚合函數
  having的語法格式與where一模一樣,只不過having是在分組之后進行的進一步過濾
    即where不能用聚合函數,而having是可以用聚合函數,這也是他們倆最大的區別
    #統計各部門年齡在30歲以上的員工平均工資,並且保留平均工資大於10000的部門
    select post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000;
    #強調:having必須在group by后面使用 (不認默認分組)
    select * from emp having avg(salary) > 10000; #報錯

 

7、distinct去重 (在having之后執行,和post,name等屬於同一執行級別)
select distinct post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000;

 

8、order by 排序 (默認升序)
select * from emp order by salary asc; #默認升序排
  select * from emp order by salary desc; #降序排
  select * from emp order by age desc; #降序排
  select * from emp order by age desc,salary asc; #先按照age降序排(優先),(有相同的age 工資)再按照薪資升序排
  # 統計各部門年齡在10歲以上的員工平均工資,並且保留平均工資大於1000的部門,然后對平均工資進行排序
  select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);

 

9、limit 限制顯示條數;分頁
select * from emp limit 3;
  select * from emp order by salary desc limit 1;  #顯示薪資最高人的信息
  select * from emp limit 0,5; #分頁, 從0開始,取5條(1-5)
  select * from emp limit 5,5; #分頁, 從5開始,取5條(6-10

 

10、正則表達式
select * from emp where name regexp '^jin.*(n|g)$';  #調正則;正則表達式通用

 

二.多表查詢(連表查詢)

1、笛卡爾積(原理)
select * from emp,dep order by emp_id,dep_id
理解:
  就是把emp中的一條記錄對應dep中的每條記錄   如果emp中只有一條記錄比如a ,dep有3條記錄比如1
2 3   它們組成的表是一個拼在一起的表: 結果:
  emp_id dep_id    a
1 a 2 a 3

 

 
2、內連接:把兩張表有對應關系的記錄連接成一張虛擬表
 select * from emp inner join dep on emp.dep_id = dep.id;
 #應用:
    select * from emp,dep where emp.dep_id = dep.id and dep.name = "技術"; # 不推薦;不要用where做連表的活
    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   #去重union與union all的區別:union會去掉相同的紀錄
 select * from emp right join dep on emp.dep_id = dep.id;

 

6、多表連接可以是單表不斷地與虛擬表連接
#查找各部門最高工資
 select t1.* from emp as t1 
 inner join
 (select post,max(salary) as ms from emp group by post) as t2  #把虛擬表提成t2
 on t1.post = t2.post
 where t1.salary = t2.ms
 ;
 select t1.* from emp as t1 
 inner join
 (select post,max(salary) as ms from emp group by post) as t2
 on t1.salary = t2.ms
 ;

 

三.子查詢(一個問題一個問題解決)

 
把一個查詢語句用括號括起來,當做另外一條查詢語句的條件去用,稱為子查詢
 select name from emp where dep_id = (select id from dep where name="技術");  #子查詢
 select emp.name from emp inner join dep on emp.dep_id = dep.id where  dep.name="技術";  #鏈表
 #查詢平均年齡在25歲以上的部門名
 select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25);   #子查詢
 select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg(age) > 25;  #鏈表
 #查看不足2人的部門名(子查詢得到的是有人的部門id)
 select * from emp where exists (select id from dep where id > 3); #exists用法,當()返回True時,外層查詢語句將進行查詢;當返回值為False時,外層查詢語句不進行查詢(empty set)
 #查詢每個部門最新入職的那位員工
 select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date 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;

 

 


免責聲明!

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



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