mysql
来源:互联网 发布:base64 python实现 编辑:程序博客网 时间:2024/06/06 03:02
/*
-- emp表
create table emp(ename varchar(20),empno int,sal float,deptno int);
--dept表
create table dept(deptno int,dname varchar(20),loc varchar(20));
--salgrade表
create table salgrade(grade int,losal float,hisal float);
--向emp中添加数据
insert into emp values('张三',1,1000.0,1);
insert into emp values('李四',2,1050.0,2);
insert into emp values('王五',3,2000.0,1);
insert into emp values('赵六',4,2050.0,3);
--向dept中添加数据
insert into dept values(1,'生活部','武汉');
insert into dept values(2,'文艺部','江夏');
insert into dept values(3,'会计部','汉口');
insert into dept values(4,'人事部','上海');
--向salgrade中添加数据
insert into salgrade values(1,5000.0,1000.0);
insert into salgrade values(2,1001.0,2000.0);
*/
/*
select * from emp;
select * from dept;
select * from salgrade;
*/
/*
select * from emp,dept,salgrade;
--笛卡尔查询
--单表查询
--1.查询最高薪水
--集合函数
select max(sal),min(sal),avg(sal) from emp;
--查出总人数
select count(empno) from emp;
select count(*) from emp;
--查出每个部门有多少人
select count(*) from emp group by deptno;
--select count(*),dname from emp group by deptno;--错误
select count(*),deptno from emp group by deptno;
--group by只能和集合函数max,min,avg,count查询字段,只有出现集合函数才能用group by
--如果上面的条件出现了,我们group by后面出现什么字段,select后面就可以查询什么字段
--查出每个部门有多少人并且人数超过一人!
select count(*),deptno from emp group by deptno having count(*)>1;
--查出部门号>1的有多少人!
select count(*),deptno from emp group by deptno having deptno>1;
--注意:having 只能和group by连用,having只能出现在group by后面
--查出每个部门有多少人并且人数超过一人而且工资超过1000!
select count(*),deptno from emp where sal>1000 group by deptno having count(*)>1;
--注意:where VS having
--where 分组前 having分组后
--不能出现集合函数 集合+字段
--从高到低查询员工的薪水
select sal from emp order by sal desc;
select * from emp order by sal desc;
--从高到低查询员工的薪水,如果薪水一样,按照deptno从高到低排序
insert into emp values('wepull',6,2000.0,4);
select * from emp order by sal desc,deptno desc;
delete from emp where deptno=4;
--二表查询
--select * from emp,dept;
--联合查询:分为 ——内连接inner join table on[where]+外连接
select * from emp,dept where emp.deptno=dept.deptno;
select * from emp inner join dept on emp.deptno=dept.deptno;
--注意:内连接的弊端——不能查询部门中为空的字段
--外连接:左外连接 + 右外连接
--left[right] 【outer】 join table on
select * from emp left join dept on emp.deptno=dept.deptno;
select * from emp right join dept on emp.deptno=dept.deptno;
--查询员工的名字和员工所在部门的位置
select ename,loc from emp,dept where emp.deptno=dept.deptno;
--select ename,loc from emp as e,dept as d where e.deptno=d.deptno;
--select ename,loc from emp e,dept d where e.deptno=d.deptno;
*/
--查询员工的名字和员工的部门所在位置并且sal>2000
--select ename,loc,sal from emp e,dept d where e.deptno=d.deptno and sal>2000;
--查询员工的名字和员工的部门所在位置并且sal是2000【和】1000
--关键字【in】
--select ename,loc,sal from emp e,dept d where e.deptno=d.deptno and sal in(2000,1000);
--查询员工的名字和员工的部门所在位置并且sal是2000【到】1000
--关键字【between and】
--select ename,loc,sal from emp e,dept d where e.deptno=d.deptno and sal between 1000 and 2000;
--查询员工的名字和员工的部门所在位置并且sal是二级工资
select ename,loc from emp e,dept d where e.deptno=d.deptno and sal between (select losal from salgrade where grade=2)
and (select hisal from salgrade where grade=2);
select ename,loc,sal from emp e,dept d,salgrade s where e.deptno=d.deptno and sal between s.losal and s.hisal and s.grade=2;
-- emp表
create table emp(ename varchar(20),empno int,sal float,deptno int);
--dept表
create table dept(deptno int,dname varchar(20),loc varchar(20));
--salgrade表
create table salgrade(grade int,losal float,hisal float);
--向emp中添加数据
insert into emp values('张三',1,1000.0,1);
insert into emp values('李四',2,1050.0,2);
insert into emp values('王五',3,2000.0,1);
insert into emp values('赵六',4,2050.0,3);
--向dept中添加数据
insert into dept values(1,'生活部','武汉');
insert into dept values(2,'文艺部','江夏');
insert into dept values(3,'会计部','汉口');
insert into dept values(4,'人事部','上海');
--向salgrade中添加数据
insert into salgrade values(1,5000.0,1000.0);
insert into salgrade values(2,1001.0,2000.0);
*/
/*
select * from emp;
select * from dept;
select * from salgrade;
*/
/*
select * from emp,dept,salgrade;
--笛卡尔查询
--单表查询
--1.查询最高薪水
--集合函数
select max(sal),min(sal),avg(sal) from emp;
--查出总人数
select count(empno) from emp;
select count(*) from emp;
--查出每个部门有多少人
select count(*) from emp group by deptno;
--select count(*),dname from emp group by deptno;--错误
select count(*),deptno from emp group by deptno;
--group by只能和集合函数max,min,avg,count查询字段,只有出现集合函数才能用group by
--如果上面的条件出现了,我们group by后面出现什么字段,select后面就可以查询什么字段
--查出每个部门有多少人并且人数超过一人!
select count(*),deptno from emp group by deptno having count(*)>1;
--查出部门号>1的有多少人!
select count(*),deptno from emp group by deptno having deptno>1;
--注意:having 只能和group by连用,having只能出现在group by后面
--查出每个部门有多少人并且人数超过一人而且工资超过1000!
select count(*),deptno from emp where sal>1000 group by deptno having count(*)>1;
--注意:where VS having
--where 分组前 having分组后
--不能出现集合函数 集合+字段
--从高到低查询员工的薪水
select sal from emp order by sal desc;
select * from emp order by sal desc;
--从高到低查询员工的薪水,如果薪水一样,按照deptno从高到低排序
insert into emp values('wepull',6,2000.0,4);
select * from emp order by sal desc,deptno desc;
delete from emp where deptno=4;
--二表查询
--select * from emp,dept;
--联合查询:分为 ——内连接inner join table on[where]+外连接
select * from emp,dept where emp.deptno=dept.deptno;
select * from emp inner join dept on emp.deptno=dept.deptno;
--注意:内连接的弊端——不能查询部门中为空的字段
--外连接:左外连接 + 右外连接
--left[right] 【outer】 join table on
select * from emp left join dept on emp.deptno=dept.deptno;
select * from emp right join dept on emp.deptno=dept.deptno;
--查询员工的名字和员工所在部门的位置
select ename,loc from emp,dept where emp.deptno=dept.deptno;
--select ename,loc from emp as e,dept as d where e.deptno=d.deptno;
--select ename,loc from emp e,dept d where e.deptno=d.deptno;
*/
--查询员工的名字和员工的部门所在位置并且sal>2000
--select ename,loc,sal from emp e,dept d where e.deptno=d.deptno and sal>2000;
--查询员工的名字和员工的部门所在位置并且sal是2000【和】1000
--关键字【in】
--select ename,loc,sal from emp e,dept d where e.deptno=d.deptno and sal in(2000,1000);
--查询员工的名字和员工的部门所在位置并且sal是2000【到】1000
--关键字【between and】
--select ename,loc,sal from emp e,dept d where e.deptno=d.deptno and sal between 1000 and 2000;
--查询员工的名字和员工的部门所在位置并且sal是二级工资
select ename,loc from emp e,dept d where e.deptno=d.deptno and sal between (select losal from salgrade where grade=2)
and (select hisal from salgrade where grade=2);
select ename,loc,sal from emp e,dept d,salgrade s where e.deptno=d.deptno and sal between s.losal and s.hisal and s.grade=2;
- Mysql
- MySql
- MYSQL
- MySQL
- mysql
- mysql
- mysql
- mysql
- Mysql
- Mysql
- mysql
- MySQL
- mysql
- Mysql
- mysql
- mysql
- mysql
- mySQL
- <s:iterator value="#departmentList">中的获取值得方法
- win7 64bit下建立jdk环境
- hdu 1576 A/B
- 路由的相关知识
- ubuntu12.10下安装Asterisk
- mysql
- C++内存管理
- php 按行读取文件
- opencv用SVM进行多类分类
- C++ 事件机制实现
- Ubuntu下安装Matlab步骤
- Hbase Java API详解
- 混世大学
- C语言中的volatile关键字