织梦网站修改幻灯片,上海装修找哪家公司好,玉田县建设局网站,企业网查询系统语法的执行顺序
select 4 字段列表
from 1 表名列表
where 2 条件列表
group by 3 分组前过滤
having 分组后过滤
order by 5 排序字段列表
limit 6 分页参数 聚合函数
count 统计数量
max 最大值
min 最小值
avg 平均
sum 总和 分组查询使…语法的执行顺序
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 workaddresscount*from emp where age45 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 函数
字符串函数
concats1,s2,s3字符串的拼接
lowerstr 小写 upperstr大写 lpadstr,n,pad
左填充用字符串pad对左边进行填充达到n个字符串长度 rpadstrnpad
右填充
trimstr
去掉字符串头部和尾部的空格
substringstrstartlen
返回字符串str从str位置起的len个长度的字符串 如
int类型不能补0因为是整形但可以补1 数值函数
ceilx 向上取整
floorx 向下取整 modX,Y 返回x/y的模 rand返回0-1内的随机数 roundX,Y四舍五入保留y位小数 日期函数
curdate() 日期
curtime() 时间
now() 现在 yeardate 获取指定date的年份
monthdate 获取指定date的月份 daydate 日期 date-adddateinterval exprtype
返回这个日期加上一个时间间隔后的时间值 datediffdate1date2
返回起始时间date1和结束时间date2之间的天数 流程函数
ifvaluetf
true返回t
false返回f ifnullvalue1,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字段2values(值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 salary5000 union all select *from emp where age50
但是结果会有重复的为了去重
可以把all去 报错情况
select *from emp where salary5000
union all
select name from emp where age50
这个会发现报错
因为对于联合查询来说。字段表的列数和字段类型必须保持一致 子查询
子查询
又称为 嵌套查询
标量子查询
查询销售部的所有员工信息
1. select id from emp where name销售部;
第一部查询出销售部id等于4
2.select *from emp where dept_id4;
要两条指令但我们想用一条搞定
select *from emp where dept_idselect 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_id3 b查询比财务部所有人工资都高的员工信息
select *from emp where salary all( select salary from emp where dept_id3) 3
查询比研发部其中任意一人工资都高的员工信息
因为是任意一人所以 没有all 行子查询
查询与‘张无忌’薪资以及领导都相同的员工的信息 a.查询张无忌的工资及其领导
select salary,managerid from emp where name张无忌 b. 查询员工
select *from emp where salary12500 and mangerid 1;
或者
select *from emp where salarymanagerid)(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 entrydate2006-01-01
把上面那个作为临时表
select e.*,d.* from(select * from emp where entrydate2006-01-01) e left join dept d on e.dept_idd.id 多表联查 1.查询员工的姓名年龄职位部门信息隐式内连接
表emp dept
连接条件emp.dept_iddept.id
记得消除笛卡尔积
select e.name,e.age,e.job,d.name from emp e, dept d where e.dept_idd.id; 2.查询年龄小于30岁的员工的姓名年龄职位部门信息显示内连接
select e.name,e.age,e.job,d.name from emp e inner join dept d on e.dept_idd.id where e.age30 3.查询拥有员工的部门id和部门名称
求取员工表和部门表之间的交集用内连接
select d.id,d.name from emp e,dept d where e.dept_idd.id
此时会有多个重复的部门因为他是按照员工数量来的
去重复用 distinct
select distinct d.id,d.name from emp e,dept d where e.dept_idd.id 4.查询所有年龄大于40的员工及其归属部门的名称如果员工没有分配部门也要显示出来
要用外连接
select e.*,d.name from emp e left join dept d on e.dept_idd.id where e.age40 5.查询所有员工的工资等级
表:emp salarygrade
连接条件emp.salary salagrade.losal and emp.salarysalagrade.hisal select e.*,s.grade emp e,salagrade s where e.salarys.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_iddept.id 查询条件 dept.name研发部 select e.*,s.grade from emp e ,dept d,salgrade s where e.dept_idd.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_idd.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_id1 select avg(e.salary) from emp e where e.dept_id2 b.
select *from emp e2 where salary(select avg(e.salary) from emp e where e.dept_ide2.dept_id)
保证平均下来的薪资是同一个部门的 11.查询所有的部门信息并统计部门的员工人数
a.查询信息
select id,name from dept
b.查询指定部门的人数
select count(*) from emp where dept_id1 最终
select d.id ,d.name (select count(*) from emp e where e.dept_idid)人数 from dept d; 12.查询所有学生的选课情况展示出学生的名称学号课程名称 表student ,course,student_course
连接条件student.idstudent_course.studentid,course.idstudent_course.courseid select s.name ,s.no,c.name from student s,student_course sc,course c where s.idsc.studentid and sc.courseidc.id