SELECT select的完整語法: 上述如果都有:執行順序from->where->group by->having->order by->limit->select 列的結果顯示 1、去掉重覆的數據:distinct(針對於記錄而言,不是針對於列的數據而言) 2、運算符:+、-、*、/、%(只 ...
SELECT
select的完整語法:
select col1, col2,... # 業務查詢的欄位 from table_name # 選取的哪張表 [where single_conditions] # single_conditions條件表達式,個體約束(條件) [[group by column_name1] # column_name1以哪個欄位名分組 [having group_conditions]] # group_conditionds條件表達式,分組約束 [order by column_name2] # column_name2以哪個欄位進行排序 [limit N,M] # 執行完之後,跳過N條記錄,選取M條記錄
上述如果都有:執行順序from->where->group by->having->order by->limit->select
列的結果顯示
1、去掉重覆的數據:distinct(針對於記錄而言,不是針對於列的數據而言)
# 查看員工的職位 select title from s_emp; select distinct title from s_emp; # 每個部門下有哪些職位 select dept_id,title from s_emp; select distinct dept_id,title from s_emp;# 聯合唯一
2、運算符:+、-、*、/、%(只列舉一個)
# 查看員工年薪 select salary*12 from s_emp; select id,last_name,salary*12 as year_salary from s_emp; # 查看員工當前年薪以及月薪提高100美元後的年薪 select id,last_name,salary*12 old_year_salary,(salary+100)*12 as new_year_salary from s_emp;
3、列與列的數據拼接:concat(column_name1, "拼接符", column_name2[, "拼接符"])
# 查看員工基本信息(編號,全名,工資) select id,concat(first_name,' ',last_name) as name,salary from s_emp;
4、將null值轉換為特定值:ifnull(column_name, 特定值)
# 查看員工工資(沒有工資的顯示為0) select id,last_name,ifnull(salary,0.00) as salary from s_emp;
where conditions
MySQL的運算符概念及作用:
主要是根據conditions的條件查詢結果集
1、比較運算符:=、<、>、>=、<=、!=、<=>(同is null)、<>(同!=)
# 查看擁有白領工資的員工有哪些(1200,2000) select id,last_name,salary from s_emp where salary>1200 and salary<2000; # 查看有工資的員工信息 select id,last_name,salary from s_emp where salary is not null; select id,last_name,salary from s_emp where salary <> null;# erro # 查看沒有工資的員工的信息 select id,last_name,salary from s_emp where salary is null; select id,last_name,salary from s_emp where salary <=> null; select id,last_name,salary from s_emp where salary != 1200;# null數據未取到
2、邏輯運算符:and(&&)、or(||)、not
查看41號部門的員工信息並且工資大於1200或者43號部門工資小於2000的員工信息 select id,last_name,salary from s_emp where (dept_id=41 and salary>1200) or (dept_id=43 and salary<2000);
3、在XXX區間:between ... and ... 不在XXX區間:not between ... and ...
查看擁有白領工資的員工有哪些(1200,2000) select id,last_name,salary from s_emp where salary>1200 and salary<2000; # 下麵是閉區間 select id,last_name,salary from s_emp where salary between 1199 and 2001; #[1199,2001] # 不在區間內 select id,last_name,salary from s_emp where salary not between 1200 and 2000; #(-&,1200)or(2000,+&)
4、在集合中: in () 不在集合中:not in ()
# 查看41,42,43號部門的員工有哪些 select id,last_name,salary from s_emp where dept_id=41 and dept_id=42 and dept_id=43; # 和上面的等價 select id,last_name,salary from s_emp where dept_id in(41,42,43); # 查看不是41,42,43號部門的員工有哪些 select id,last_name,salary from s_emp where dept_id!=41 and dept_id!=42 and dept_id!=43; # 和上面的等價 select id,last_name,salary from s_emp where dept_id not in(41,42,43)
6、模糊匹配:like :%:0到多個字元匹配;_:1個字元匹配;[ ]:範圍內的匹配的單個字元;[^ ]:範圍外的匹配的單個字元; 不模糊匹配: not
# 查看職位以VP開頭的員工有哪些 select id,last_name,salary from s_emp where title like 'VP%'; select id,last_name,title from s_emp where title not like 'VP%'; # 查看員工信息,名字以C開頭,並且字元數不小於5個字元 select id,last_name from s_emp where last_name like 'C____%'; #查看客戶信息,客戶名稱中包含單引號的客戶 select id,name from s_cutomer where name like "%'%";
group by column_name
涉及的組函數:計數count()、最小值min()、最大值max()、平均值avg()、總和sum()
# 查看員工總數 select count(*) as count_num from s_emp; # 預設分組(以表格為單元) select count(id) as count_num from s_emp; # 統計有工資的員工個數 select count(salary) as count_num from s_emp; # 查看每個部門的員工個數 select dept_id,count(*) as nums from s_emp group by dept_id; # 進行分組後,select的結果只能是組的概念,不允許出現個體概念(last_name) select dept_id,count(*) as nums,last_name from s_emp group by dept_id; # errro # 預設以逗號拼接 group_concat(),這個函數很重要 select dept_id,count(*) as nums,group_concat(last_name) from s_emp group by dept_id; # 查看每個部門薪資大於1200的員工總數(信息) select dept_id,count(*) nums,group_concat(last_name),group_concat(salary) from s_emp where salary > 1200 group by dept_id; # 查看部門平均薪資 select avg(salary) from s_emp group by dept_id; # 查看部門平均薪資>2000員工總數 select dept_id,count(*) nums,avg(salary) from s_emp group by dept_id having avg(salary)>2000; # 查看每個部門員工總數,部門平均薪資大於1000,並且每個員工的薪資>900 select dept_id,count(*),avg(salary) from s_emp where salary >900 group by dept_id having avg(salary)>1000
排序order by:升序ASC;逆序DESC
# 查看員工的員工ID,名字,月薪,部門ID,部門ID進行升序排序,相同部門的員工在一起按照薪資從高到低排序 select id,last_name,dept_id,salary from s_emp order by dept_id asc,salary desc; # 如果進行排序的時候,需要對欄位先進行轉碼,後排序 select id,last_name,dept_id,salary from s_emp order by conver(dept_id asc,salary using gbk2312) desc;
限制記錄數目:limit N,M 跳過N條記錄,查詢M條記錄
# 跳過3條記錄,查詢5條記錄,這一般用於分頁比較合理 # 擦昏地當前頁數和記錄數 select id,last_name,dept_id,salary from s_emp order by dept_id asc,salary desc limit 3, 5 # 當數據表的記錄上萬條,比如超過1萬條記錄 # 建議使用子查詢進行分頁,這樣效率高點,因為子查詢是在索引文件(所以你文件比較小,數據文件比較大,處理上就比較慢)上執行的 select id, last_name, dept_id, salary from s_emp where id >=( select id from s_emp order by id limit 10000, 1 ) limit 10;