MySQL單表查詢

關鍵字執行的優先級

from      # 先找到表from
where     # where約束條件,在文件中找記錄,不能和聚合函數合用
group by  # 對記錄進行分組,若是沒有,就全部字段是一組
select   # 執行select
distinct  # 若是沒有group by,就用distinct去重,放在select以後
having    # 進行having過濾,只有group by使用時才用having過濾
order by  # 按條件進行排序
limit    # 顯示查詢結果顯示的條數

建表和數據準備

一、建表ide

字段名                  數據類型                說明
員工id:id                int            primary key,auto_increment
姓名:name                varchar                not null
性別:gender           enum("male","female")      not null,default "male" 
年齡:age                 int(3)         not null,default 24,unsigned
入職日期:hire_date            date                not null
崗位:post               varchar(50)    
職位描述:post_comment        varcahr(100)    
薪水:salary              double(15,2)    
辦公室:office               int               一個部門一間辦公室
部門編號:depart_id            int    

二、插入數據函數

insert into employee(name, gender, age, hire_date, post, post_content, salary, office, depart_id) values 
('小趙','male',78,'20150302','sale',13000.31,401,1),
('小錢','male',81,'20130305','sale',9000,401,1),
('小孫','male',73,'20140701','sale',9600,401,1),
('小李','male',28,'20121101','sale',8600,401,1),
('小周','female',18,'20110211','hr',6000,403,2),    # 人力資源
('小吳','female',18,'19000301','hr',6500,403,2),
('小鄭','female',48,'20101111','hr',6000,403,2),
('小王','female',48,'20101111','hr',7000,403,2),
('小馮','male',48,'20101111','it',200000,405,3),    # 程序猿
('小陳','male',48,'20101111','it',16000,405,3),
('小褚','male',48,'20101111','it',18000,405,3),
('小衛','male',48,'20101111','it',14000,405,3),

簡單查詢

# 查詢指定字段
select name,hire_date,post,salary,office,depart_id from employee;
# 用*查詢表中全部數據
    select * from employee;    
# 避免重複distinct
    select distinct post from employee;
# 經過四則運算查詢
    select name, salary*12 from employee;
    # 計算出年薪並做爲一個新字段顯示
    select name, salary*12 as annual_salary from employee;
    # 查詢年薪
    select name, salary, annual_salary from employee;
# 定義顯示格式
    # concat(),用於鏈接字符串
    select concat('姓名:', name,'   薪資:', salary) from employee;
    # concat_ws(),第一個參數是分割符
    select concat_ws(':', name, salary) as annual_salary from employee;
# 結合case語句使用:
    select 
    ( case                  # case,開始一個條件語句 when name
= '小周' then name    # when 條件1 then 字段操做 when name = '小王' then concat(name,'_BIGSB') else concat(name, 'SB')       # else, 字段操做 end                   # 結束條件語句 ) as new_name from employee;

where約束

比較運算符:>    <    >=    <=    !=    <>    # 對值進行判斷
between a and b        # 值在a-b之間, 對範圍進行判斷,是一個閉區間
in(10,20,30)           # 值是10,20或30
like '%'               # 通配符能夠是%(任意多個字符),_(一個字符)
    like 'a%'    # 以a開頭的字符
    like '%a'    # 以a結尾的字符
    like '%a%'  # 中間包含a的字符
    like '_a'    # 任意一個字符+a
    like 'a_'    # a+任意一個字符
where中的關鍵字
# 單條件查詢
    select name from employee where post='sale';

# 多條件查詢
    select name, salary from employee where post='sale' and salary>7000;

# 關鍵字between and
    select name, salary from employee where salary between 10000 and 16000;
    select name, salary from employee where salary not between 10000 and 16000;

# 關鍵字is null
    select name,post_content from employee where post_content is null;
    select name,post_content from employee where post_content is not null;
    select name,post_content from employee where post_content =''; 注意''是空字符串,不是null
    ps:須要執行 update employee set post_comment='' where id=2;以後,再用上條查看,就會有結果了

# 關鍵字in
    select name,salary from employee where salary=10000 or salary=16000 or salary=18000;
    select name,salary from employee where salary in (8600,16000,18000);
    select name,salary from employee where salary not in (8600,16000,18000);

# like模糊查詢
    # 通配符%
    select * from employee where name like '小%'# 通配符_
    select * from employee where name like '小_'
查詢示例

group by

# 單獨使用group by
    select post from employee group by post;
    # 按照post字段分組,select查詢的字段只能是post,要獲取其餘信息,須要藉助函數來實現;

# group by 和 group_concat()聯合使用
    select post, group_concat(name) from employee group by post;
    # 按照崗位分組,並展現組內成員
    select post, group_concat(name) as emp_members from employee group by post;    # 按照崗位分組,將組內成員做爲新字段展現

# group by和 聚合函數 一塊兒使用
    select post, count(id) as count from employee broup by post;
    # 按照崗位分組,並查看每一個組中有多少人

# ps:若是用unique字段做爲分組依據,則每條記錄自成一組,這樣的分組沒有意義

聚合函數

# 聚合函數聚合的是組的內容,若是沒有分組,默認是一組

select count(*) from employee;
select count(id) from employee where dapart_id=3;

select max(salary) from employee;
select min(salary) from employee;
select avg(salary) from employee;
select sum(salary) from employee;
select sum(salary) from employee where dapart_id=1;

having過濾

# having和where不同的地方在於:
    執行優先級:where > group by > having
# where 發生在group by以前,能夠有任意字段,但絕對不能使用聚合函數;
# having函數發生在group by以後,可使用分組的字段,不能取到其餘字段,可使用聚合函數;

order by排序

# 按單列進行排序
    select * from employee order by salary;
    select * from employee order by salary asc;      # 升序
    select * from employee order by salary desc;     # 降序
# 按多列排序:先按age排序,若是年紀相同,按薪資排序
    select * from employee order by age, salary desc;

limit限制查詢的記錄

select * from employee order by salary desc limit 3;# 默認初始位置0
select * from employee order by salary desc limit 0,5# 從0開始,先查詢第一條,包含這一條在內日後查詢5條;
select * from employee order by salary desc limit 5,5;
    # 從5開始,先查詢第六條,日後查詢5條;
相關文章
相關標籤/搜索