語法的執行順序
?
select 4 字段列表
?
from 1 表名列表
?
where 2 條件列表
?
group by 3 分組前過濾
?
having 分組后過濾
?
order by 5 排序字段列表
?
limit 6 分頁參數
聚合函數
?
count 統計數量
?
max 最大值
?
min 最小值
?
avg 平均
?
sum 總和
分組查詢使用例子
1.性別分組,統計數量
?
select gender ,count(*)from emp group by gender
?
?
2.性別分組 統計平均年齡
?
select age ,avg(age) from emp group by gender
?
?
3.查詢年齡小于45的員工,并且按照工作地址分組,獲取員工數量>=3的工作地址
?
首先
?
按工作地址分組然后獲取年齡小于45的員工的地址信息的總數
?
select workaddress,count(*)from emp where age<45 group by workaddress
?
再分組完后進行過濾
?
having count(*)>=3
排序查詢使用例子
語法:
?
select 字段列表 from 表名 order by 字段1 排序方式1,字段2,排序方式2;
?
?
排序方式:
?
1.ASC:升序(默認)
?
2.DESC:降序
?
?
例子
?
1.根據年齡進行排序,2.年齡相同,再入職日期降序排序
?
select age from emp order by ageasc,entrydate desc
?
多字段排序:第一個字段相同時再進行第二個字段排序
分頁查詢
?
語法
?
select 字段列表 from 表名 limit 起始索引,查詢記錄數;
?
select * from emp limit 0 10
函數
字符串函數
?
concat(s1,s2,s3)字符串的拼接
?
lower(str) 小寫
?
upper(str)大寫
?
?
lpad(str,n,pad)
?
左填充,用字符串pad對左邊進行填充,達到n個字符串長度
?
?
rpad(str,n,pad)
?
右填充
?
trim(str)
?
去掉字符串頭部和尾部的空格
?
substring(str,start,len)
?
返回字符串str從str位置起的len個長度的字符串
?
?
如
?
int類型不能補0,因為是整形但可以補1
?
數值函數
?
ceil(x) 向上取整
?
floor(x) 向下取整
?
?
mod(X,Y) 返回x/y的模
?
?
rand()返回0-1內的隨機數
?
?
round(X,Y)四舍五入,保留y位小數
?
日期函數
?
curdate() 日期
?
curtime() 時間
?
now() 現在
?
year(date) 獲取指定date的年份
?
month(date) 獲取指定date的月份
?
?
day(date) 日期
?
?
date-add(date,interval exprtype)
?
返回這個日期加上一個時間間隔后的時間值
?
?
datediff(date1,date2)
?
返回起始時間date1和結束時間date2之間的天數
流程函數
?
if(value,t,f)
?
true返回t
?
false返回f
?
?
ifnull(value1,value2)不空返回value1,空的話返回value2
?
?
case when then
?
?
case when [val]then [res1] else [defaulse] End
?
?
val為true則返回res1
?
否則返回default默認值
?
?
case [expr] when [val] then [res1] else [default] End
?
end是結束
?
當expr的值等于val時返回res1否則返回default
?
?
使用例子
?
select name,(case workaddress when'北京' then'一線',when‘上海’,then‘一線’ end)as‘工作地址’
?
增刪改查
添加數據
?
1.給指定字段添加數據 insert values
?
insert into 表名(字段1 , 字段2) values(值1,值2)
?
?
?
2.給全部字段添加數據(不寫出具體字段名)
?
insert into 表名 values(值1,值2)
?
?
3.批量添加數據
?
給特定字段
?
insert into 表名(字段1,字段2)values(值1,值2)(值1,值2);
?
給全部字段
?
insert into 表名 values (值1,值2), (值1,值2), (值1,值2);
更新與刪除
?
修改數據:update set
?
update 表名 set 字段名1=值1,字段名2=值2......[where 條件]
?
不寫條件where的話就是所有都執行
?
?
刪除數據 delete
?
delete from 表名 [where 條件]
?
聯合查詢(union)
union查詢,就是把多次的查詢結果合并起來形成一個新的查詢結果
?
select 字段列表 from 表a
?
union[all]
?
select 字段列表 from 表b
?
?
分別查詢薪資>5000,年齡>50的員工
?
select *from emp where salary>5000
?
?
union all
?
?
select *from emp where age>50
?
但是結果會有重復的,為了去重
?
可以把all去
?
報錯情況
select *from emp where salary>5000
?
union all
?
select name from emp where age>50
?
這個會發現報錯
?
因為對于聯合查詢來說。字段表的列數和字段類型必須保持一致
?
子查詢
子查詢
?
又稱為 嵌套查詢
?
標量子查詢
?
查詢銷售部的所有員工信息
?
1. select id from emp where name='銷售部';
?
第一部查詢出銷售部id等于4
?
2.select *from emp where dept_id=4;
?
要兩條指令,但我們想用一條搞定
?
select *from emp where dept_id=(select id from emp where name='銷售部');
?
?
列子查詢
?
常用操作符:
in,not in,any,some,all
?
1
?
select id from dept where name='銷售部'or name='市場部';
?
查出的id是1和2
?
然后
?
select* from emp where dept_id in (1,2)
?
或者
?
select* from emp where dept_id in (select id from dept where name='銷售部'or name='市場部');
?
2
?
查詢比財務部的所有人工資都高的員工的 信息
?
a 查詢所有財務部人員的工資
?
select id from dept where name='財務部';
?
select salary from emp where dept_id=3
?
?
b查詢比財務部所有人工資都高的員工信息
?
select *from emp where salary> all( select salary from emp where dept_id=3)
?
?
3
?
查詢比研發部其中任意一人工資都高的員工信息
?
因為是任意一人所以 沒有all
?
行子查詢
?
查詢與‘張無忌’薪資以及領導都相同的員工的信息
?
a.查詢張無忌的工資及其領導
?
select salary,managerid from emp where name='張無忌'
?
b. 查詢員工
?
select *from emp where salary=12500 and mangerid =1;
?
或者
?
select *from emp where (salary,managerid)=(12500,1);
?
再或者
?
select *from emp where(salary,mangerid)=(select salary,managerid from emp where name='張無忌')
?
?
表子查詢
?
常用操作符 in
?
1.查詢與‘路’和‘白’薪資以及職位相同的員工
?
select job,salary from emp where name='路'or name=‘白’
?
select * from emp where (job,salary) in(select job,salary from emp where name='路'or name=‘白’)
?
?
2.查詢入職日期是“2006-01-01”之后的員工信息,及其部門信息
?
select * from emp where entrydate>"2006-01-01"
?
把上面那個作為臨時表
?
select e.*,d.* from(select * from emp where entrydate>"2006-01-01") e left join dept d on e.dept_id=d.id
?
多表聯查
1.查詢員工的姓名,年齡,職位,部門信息(隱式內連接)
?
表:emp dept
?
連接條件:emp.dept_id=dept.id
?
記得消除笛卡爾積
?
select e.name,e.age,e.job,d.name from emp e, dept d where e.dept_id=d.id;
?
?
2.查詢年齡小于30歲的員工的姓名,年齡,職位,部門信息(顯示內連接)
?
select e.name,e.age,e.job,d.name from emp e inner join dept d on e.dept_id=d.id where e.age<30
?
?
3.查詢擁有員工的部門id和部門名稱
?
求取員工表和部門表之間的交集用內連接
?
select d.id,d.name from emp e,dept d where e.dept_id=d.id
?
此時會有多個重復的部門,因為他是按照員工數量來的
?
去重復用 distinct
?
select distinct d.id,d.name from emp e,dept d where e.dept_id=d.id
?
?
4.查詢所有年齡大于40的員工,及其歸屬部門的名稱;如果員工沒有分配部門也要顯示出來
?
要用外連接
?
select e.*,d.name from emp e left join dept d on e.dept_id=d.id where e.age>40
?
?
5.查詢所有員工的工資等級
?
表:emp salarygrade
?
連接條件:emp.salary >=salagrade.losal and emp.salary<=salagrade.hisal
?
?
select e.*,s.grade emp e,salagrade s where e.salary>=s.losal and e.salary <=s.hisal
?
?
第二種寫法:
?
select e.*,s.grade emp e,salagrade s where e.salary between s.losal and s.hisal
?
?
6.查詢 研發部 所有員工的信息以及工資等級
?
涉及到的表:emp dept salgrade
?
連接條件:
?
emp.salary between s.losal and s.hisal
?
?
emp.dept_id=dept.id
?
?
查詢條件 dept.name='研發部'
?
?
select e.*,s.grade from emp e ,dept d,salgrade s where e.dept_id=d.id and ( emp.salary between s.losal and s.hisal)and d.name='研發部'
?
?
7.查詢研發部員工的平均工資
?
表 emp dept
?
select avg(e.salary) from emp e, dept d where e.dept_id=d.id and e.name='研發部'
?
?
8.查詢工資比‘滅絕’高的員工信息
?
select * from emp where salary>(select salary from emp where name='滅絕')
?
查詢滅絕的薪資
?
select salary from emp where e.name='滅絕'
?
?
9.查詢比平均薪資高的員工信息
?
select avg(salary) from emp
?
select * from emp where salary>(select avg(salary) from emp)
?
?
10.查詢低于 本部門 平均薪資的員工
?
a.查詢指定部門的平均薪資
?
select avg(e.salary) from emp e where e.dept_id=1
?
select avg(e.salary) from emp e where e.dept_id=2
?
?
b.
?
select *from emp e2 where salary<(select avg(e.salary) from emp e where e.dept_id=e2.dept_id)
?
保證平均下來的薪資是同一個部門的
?
?
11.查詢所有的部門信息,并統計部門的員工人數
?
a.查詢信息
?
select id,name from dept
?
b.查詢指定部門的人數
?
select count(*) from emp where dept_id=1
?
?
最終
?
select d.id ,d.name (select count(*) from emp e where e.dept_id=id)'人數' from dept d;
?
?
?
12.查詢所有學生的選課情況,展示出學生的名稱,學號,課程名稱
?
?
表:student ,course,student_course
?
連接條件:student.id=student_course.studentid,course.id=student_course.courseid
?
?
select s.name ,s.no,c.name from student s,student_course sc,course c where s.id=sc.studentid and sc.courseid=c.id
?