1. 程式人生 > >手擼Mysql原生語句--單表

手擼Mysql原生語句--單表

`select from where group by having order by limit` 上面的所有操作是有執行的優先順序的順序的,我們將執行的過程可以總結為下面所示的七個步驟。 1.找到表:from 2.拿著where指定的約束條件,去檔案/表中取出一條條記錄 3.將取出的一條條記錄進行分組group by,如果沒有group by,則整體作為一組 4.執行select(去重) 5.將分組的結果進行having過濾 6.將結果按條件排序:order by 7.限制結果的顯示條數 ### select from的使用 簡單查詢 `SELECT id,emp_name,sex,age,hire_date,post,post_comment,salary,office,depart_id FROM employee;` `SELECT * FROM employee;` `SELECT emp_name,salary FROM employee;` 避免重複DISTINCT `SELECT DISTINCT post FROM employee;` 通過四則運算: `SELECT emp_name, salary*12 FROM employee;` `SELECT emp_name, salary*12 AS Annual_salary FROM employee;` `SELECT emp_name, salary*12 Annual_salary FROM employee;` 1.查出所有員工的名字,薪資,格式為:<名字:egon> <薪資:3000> `select concat('<名字:',emp_name,'> ','<薪資:',salary,'>') from employee;` 2.查詢所有的崗位(去掉重複的崗位) `select distinct depart_id from employee;` 3.查詢所有員工的名字以及他們的年薪,年薪的欄位名為annual_year `select emp_name, salary*12 annual_year from employee;` ### where約束 1.檢視崗位是teacher的員工的姓名、年齡 `select emp_name,age from emp_name where post = 'teacher';` 2.檢視崗位是teacher且年齡大於30歲的員工的姓名、年齡 `select emp_name,age from employee where post ='teacher' and age >30;` 3.檢視崗位是teacher且薪資在9000-10000範圍內的員工姓名、年齡、薪資 `select emp_name,age,salary from employee where post ='teacher' and salary between 9000 and 10000;` `select emp_name,age,salary from employee where post ='teacher' and salary >=9000 and salary <=10000;` 4.檢視崗位描述不為null的員工資訊 `select * from employee where post_comment is not null;` 5.檢視崗位是teacher且薪資是10000或9000或30000的員工姓名、年齡、薪資。 `select emp_name,age,salary from employee where post = 'teacher' and salary in (10000,9000,30000);` 6.檢視崗位是teacher且薪資不是10000或9000或30000的員工姓名、年齡、薪資。 `select emp_name,age,salary from employee where post='teacher' and salary not in (10000,9000,30000);` 7.檢視崗位是teacher且名字是jin開頭的員工姓名、年薪 `select emp_name,salary*12 annual_year from employee where post = 'teacher' and emp_name like 'jin%';` `select emp_name,salary*12 annual_year from employee where post = 'teacher' and emp_name regexp '^jin';` ### group by--聚合(sum,avg,min,max,count)的使用 1.查詢崗位名及崗位包含的所有員工名字 `select post, group_concat(emp_name) from employee group by post;` `select post, group_concat(emp_name) as emp_members from employee group by post;` 2.查詢崗位名以及崗位內包含的員工個數 `select post,count(id) from employee group by post;` 3.查詢公司內男員工和女員工的個數 `select sex,count(id) from employee group by sex;` 4.查詢崗位名以及各崗位的平均薪資 `select post,avg(salary) from employee group by post;` 5.查詢崗位名以及各個崗位的最高薪資 `select post,max(salary) from employee group by post;` 6.查詢敢為名以及各個崗位的最低資訊 `select post,min(salary) from employee group by post;` 7.查詢男員工與男員工的平均薪資,女員工和女員工的平均薪資 `select sex,avg(salary) from employee group by sex;` 8.查詢崗位名以及崗位包含員工的所有薪資的總和 `select post,sum(salary) from employee group by post;` ### having過濾的使用 !!!執行優先順序從高到低:where > group by > having 1. Where 發生在分組group by之前,因而Where中可以有任意欄位,但是絕對不能使用聚合函式。 2. Having發生在分組group by之後,因而Having中可以使用分組的欄位,無法直接取到其他欄位,可以使用聚合函式 1.查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數 `select post,group_concat(emp_name),count(id) from employee group by post having count(id)<2;` 2.查詢各崗位平均薪資大於10000的崗位名、平均工資 `select post,avg(salary) from employee group by post having avg(salary) >10000;` 3.查詢各崗位平均薪資大於10000且小於20000的崗位名、平均薪資 `select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;` `select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) <20000;` ### order by查詢排序的使用 1.查詢所有員工資訊,先按照age升序排序,如果age相同則按照hire_date降序排序 `select * from employee order by age asc,hire_date desc;` 2.查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列 `select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) asc;` 3.查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列 `select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) desc;` ### limit限制查詢的記錄數 `select * from employee limit 0,5;` `select * from employee limit 5,5;` `select * from employee limit 10,5;` 備註:limit後面的兩個數字分別表示的含義為:第一個數字表示的含義是從第幾條資料開始,第二個數字表示的是需要取出幾條的資料。 ### 使用正則表示式查詢 `select * from employee where emp_name REGEXP '^ale';` `SELECT * FROM employee WHERE emp_name REGEXP 'on$';` `SELECT * FROM employee WHERE emp_name REGEXP 'm{2}';` 小結:對字串匹配的方式 `WHERE emp_name = 'egon';` `WHERE emp_name LIKE 'yua%';` `WHERE emp_name REGEXP 'on$';` 1.檢視所有員工中名字是jin開頭,n或者g結果的員工資訊。 `select * from employee where emp_name regexp '^jin.*