??xml version="1.0" encoding="utf-8" standalone="yes"?> Tomcat5版本?font face="Verdana">数据源的配置如下Q?br />
<Resource name="jdbc/mldn" auth="Container" type="javax.sql.DataSource"/> Tomcat6版本的数据源的配|如下: W一??Tomcat_HOME%conf\server.xml?font face="Verdana"> <Host>标签里当前位|下?font face="Verdana"><Context>?/font>d如下代码?/font> <Resource name="jdbc/mldn" 另外?Tomcat_HOME%conf\context.xml?font face="Verdana"> <Context>标签?font face="Verdana">?/font>d如下代码?/font> <resource-ref> 注意Q?font face="Verdana">要求<res-ref-name>jdbc/mldn</res-ref-name>中的“jdbc/mldn”一定要与在server.xml中配|的Resource中的name一_不然会出错?/font> W三步:在测试之前,要确保将数据库的驱动jar包放刎ͼ你的应用目录\WEB-INF\lib目录下和%Tomcat_HOME%c\lib目录下? 下面重启TomcatOK了!
--DML--Data Manipulation Language--数据操作语言
query information (SELECT),
add new rows (INSERT),
modify existing rows (UPDATE),
delete existing rows (DELETE),
perform a conditional update or insert operation (MERGE),
see an execution plan of SQL (EXPLAIN PLAN),
and lock a table to restrict access (LOCK TABLE).
--DDL--Data Definition Language--数据定义语言
create, modify,drop, or rename objects (CREATE,ALTER,DROP,RENAME),
remove all rows from a database object without dropping the structure (TRUNCATE),
manage access privileges (GRANT,REVOKE),
audit database use (AUDIT,NOAUDIT)
and add a description about an object to the dictionary (COMMENT).
--Transaction Control事务控制语句
save the changes(COMMIT)
or discard the changes (ROLLBACK) made by DML statements.
Also included in the transaction-control statements are statements to set a point or marker in the transaction for possible rollback (SAVEPOINT)
and to define the properties for the transaction (SET TRANSACTION).
Used to manage the properties of the database.
There isonly one statement in this category (ALTER SYSTEM).
--DCL--Data Control Language--与开发关pM是很密切,用于权限的分配与回收
grant,revoke,data control
--Session Control
control the session properties (ALTER SESSION)
and to enable/disable roles (SET ROLE).
--System Control
--------------------------------------------------------
select的用?
--每个员工的所有信?
select * from emp
--每个人的部门~号Q姓名,薪水
select deptno,ename,sal from emp;
--每个人的q薪
select ename,sal*12 from emp;
--计算2*3的?
select 2*3 from emp;
--计算2*3的?dual)
select 2*3 from dual;
select * from dual;
--得到当前旉
select sysdate from dual
--可以l列起别?比如求每个h的年?
select ename,sal*12 salperyear from emp;
--如果别名中有I格Q需要用双引?
select ename,sal*12 "sal per year" from emp;
--如果没有内容Q则为空
select comm from emp;
--当空字段参与计算Q则l果是null
--例如Q计每个h的全q的收入包括月薪和年l奖
select ename,sal*12+comm from emp;
--可以多个字W串拼在一赗比如:求每个h的薪_格式为smith-sal-123
select ename||'-sal-'||sal from emp;
--如果字符串中有单引号Q需要用另外一个单引号转义Q比如:q样一个字W串Q?he's friend
select ename||'''s sal is'||sal from emp;
--------------------------------------------------------
--distinct 关键词的用法
--求有哪些个部?
select distinct deptno from emp
--可以用来修饰多个字段。比如:求有哪些个部门和job的组?
select distinct deptno,job from emp
--------------------------------------------------------
where关键词的用法
--可以是数值类型的{值判断。比如:?0q个部门的所有员?
select * from emp where deptno=20
--可以是字W串cd的等值判断。比如:求叫KING的这个h的信?
select * from emp where ename = 'KING'
--也可以是不等值判断。比如:求薪水小?000的员工信?
select * from emp where sal<2000;
--字符串也可以做不{值判断,比如Q求所有ename大于'CBA'的员工信息?
select * from emp where ename>'CBA';
--求部门不?0的员?
select * from emp where deptno <> 10;
--求薪水在800?500之间的员工信?
select * from emp where sal >=800 and sal <=1500;
--也可以写?
select * from emp where sal between 800 and 1500
--q样写则不可?
-----------------------------select * from emp where 800<=sal<=1500
--where...in..的用法。比如:求薪水是800或?500或正2000的员工信?
select * from emp where sal=800 or sal=1500 or sal=2000
--相当于写成这?
select * from emp where sal in(1500,800,2000,1500,1500,1500,1500);
--再比如求姓名是KING,SMITH,AA的员工信?
select * from emp where ename in ('KING','SMITH','AA')
--求入职时间在20-2?81之后的员工信?
select * from emp where hiredate < '23-5?-87';
--------------------------------------------------------
--and or not的用?
--求薪水大?000或者部门在20q个部门的员工信?
select * from emp where sal>1000 and deptno=20
--求薪水不?00或者不?500或者不?000的员工信?
select * from emp where sal not in (800,1500,3000)
--也可以这h?
select * from emp where sal <>800 and sal <> 1500 and sal<>3000
--------------------------------------------------------
--like的用?
--求名字中包含ALLq三个字W的员工信息
select * from emp where ename like '%E%';
--求名字中的第二个字母是A的员?
select * from emp where ename like '_A%';
--Ҏ字符需要{义。比如:求员工中包含Ҏ字符%的员工信?
select * from emp where ename like '%\%%' escape '\'
--------------------------------------------------------
--null的用?
--求没有年l奖的员?
select * from emp where comm is null
--求有q终奖的员工
select * from emp where comm is not null
--------------------------------------------------------
--order by的用?
--员工信息按照姓名正序排列
select * from emp order by ename asc;
--员工信息按照倒叙排列
select * from emp order by ename desc;
--也可以是多个字段l合排列。例如:员工信息按照部门正序排列Qƈ且按照姓名倒叙排列
select * from emp order by deptno asc,ename desc
--------------------------------------------------------
--function的用?
--把所有姓名变成小?
select lower(ename) from emp;
--把所有姓名变成大?
select upper(ename) from emp;
--求所有h名中包含'a'的员工信息不区分大小?
select * from emp where lower(ename) like '%a%'
--截取子字W串Q比如求Hello的一部分
select substr('hello',2,2) from dual;
select substr(ename,2,2) from emp;
--求Hello的一部分Qƈ指明长度
--求ascii码对应的字符
select chr(65) from dual
--求字W对应的ascii?
select ascii('?)from dual
--四舍五入
select round(12.456,2) from dual
select round(12.456,-1) from dual
--四舍五入数点后面多位
--四舍五入数点前面多位
--------------------------------------------------------
--important!日期转换函数
--------------------------------------------------------
--当前日期{换成1981-03-12 12:00:00q种形式的字W串
select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;
--?981-03-12 12:00:00字符串{换成日期
select to_date('1981-03-12 12:00:00','YYYY-MM-DD HH24:MI:SS') from dual;
--每个h的薪水{换成固定格式的字W串
select to_char(sal,'$999,999,999.99') from emp;
--固定格式的字符串{换成数?
select to_number('$8,000.00','$999,999,999.99') from dual;
--当null参与计算时候,可以用nvlq个函数。比如求每个Zqd的收?
select ename,sal*12+comm from emp
--------------------------------------------------------
--group functionl函?
--求所有h的薪水的dQ^均|最大|最?
select sum(sal),avg(sal),max(sal) ,min(sal) from emp;
--求ȝ行数
select count(*) from emp;
--求ȝ行树Q(可以指定具体的字D)但如果字D|null值的时候需要小心?
select count(comm) from emp;
--也可以过滤掉重复的行之后l计行数
select count(distinct deptno) from emp
--可以指明按照哪个字段q行分组.比如Q分部门l计最高薪?
select deptno,max(sal) from emp where deptno is not null group by deptno
--也可以按照多个字D|分组l计Q比如:分部门和岗位Q统计最高薪水和行数
select deptno,job,max(sal),count(*) from emp group by deptno,job
--------------------------------------------------------
--重要Q出现在select列表中的字段Q如果没有在l函CQ那么必d现在group by 子句中?
--------------------------------------------------------
select ename,deptno,job,max(sal),count(*) from emp group by deptno,job
--求薪水最高的员工姓名
select * from emp where sal=(select max(sal) from emp);
delete from emp where ename='TEST2'
update emp set deptno=10 where deptno=99
select * from dept
insert into dept (deptno,dname,loc) values('10','ACCOUNTING','NEW YORK');
--having从句的用?
--求^均薪水是2000以上的部?
select deptno,avg(sal) as avg_sal from emp group by deptno
having avg(sal) >2000
--------------------------------------------------------
--ȝ一下select语法
select
from
where
group by
having
order by
--------------------------------------------------------
-- 执行序very important!
-- 首先执行where语句原有记录过滤;
-- W二执行group by q行分组Q?
-- W三执行havingqo分组Q?
-- 然后select 中的字段值选出来;
-- 最后执行order by q行排序Q?
--------------------------------------------------------
/*
按照部门分组l计Q求最高薪_q_薪水
只有薪水?200以上的才参与l计
q且分组l果中只包括q_薪水?500以上的部?
而且按照q_薪水倒叙排列
*/
select max(sal),avg(sal) from emp
where sal>1200
group by deptno
having avg(sal) >1500
order by avg(sal) desc
--------------------------------------------------------
/*
把雇员按部门分组Q?
求最高薪_ 部门P
qo掉名字中W二个字母是'A'的,
要求分组后的q_薪水>1500Q?
按照部门~号倒序排列
*/
select max(sal) ,deptno from emp where ename not like '_A%'group by deptno
having avg(sal) >1500
order by deptno desc
/* very very important! */
select ename, deptno from emp;
select deptno, dname from dept;
--------------------------------------------------------------------------------------
--老语法:----------------------------------------------------------------------------
--------------------------------------------------------------------------------------
--{D接:求员工姓名以及员工所在部门的名字同时昄出来
select ename,emp.deptno,dname,dept.deptno from emp,dept
where emp.deptno = dept.deptno
select ename,e.deptno,dname,d.deptno from emp e,dept d
where e.deptno = d.deptno
--非等D接:要求每位雇员的薪水等U?
select * from salgrade
select ename,sal,grade,losal,hisal from emp,salgrade
where sal >=losal and sal <=hisal
--?个表Q求工作职位?#8217;PRESIDENT’的雇员姓名,部门名称和薪水等U时
select ename,dname,grade from emp,dept,salgrade
where emp.deptno = dept.deptno
and sal >=losal and sal <=hisal
and job ='PRESIDENT'
--也可以同一个表做跨表连接:求每位员工的姓名Q及其上U经理的姓名
select e1.ename,e2.ename from emp e1,emp e2
where e1.mgr = e2.empno
--------------------------------------------------------------------------------------
--新语?-----------------------------------------------------------------------------
--在SQL1992的语法规则中Q语句过滤的条g和表q接的条仉被放在了where子句中,当条件过多时Q容易造成hQ?
--SQL1999修正了这个缺点,连接条件和数据qo条g区分开来,
--------------------------------------------------------------------------------------
--交叉q接
--l果会生这两张表的W卡乘U?
select * from emp cross join dept
--要用deptno作ؓ{D接条Ӟ我们可以q样?
select * from emp join dept using (deptno)
select ename, dname from emp join dept using(deptno);
--相当?
select ename, dname from emp join dept on emp.deptno = dept.deptno
--也可以写成这?
--也可以用于非{D?
--求每位雇员的薪水{
select * from emp join salgrade on (sal >=losal and sal<= hisal)
--多个join,wherel合使用
--(求工作职位是’PRESIDENT’的雇员姓名,部门名称和薪水等U时)
select * from emp join dept on emp.deptno = dept.deptno
join salgrade on (sal >=losal and sal<= hisal)
where job = 'PRESIDENT'
--外连?-取出表中q接不到一L多余的数?
--没有全内q接Q没有右内连?
--其中outer也可以省略,写ؓleft join , right join , full join
--left inner join可以~写成inner join 也可以羃写成joinQ意思是左内?
--update emp set deptno=20 where ename='SMITH';
--commit;
select * from emp;
select * from dept;
delete from dept where deptno=99;
--左内,从左往xQ匹配不上的记录不显C?
select ename,emp.deptno from emp join dept on emp.deptno = dept.deptno;
select ename,emp.deptno from emp inner join dept on emp.deptno = dept.deptno;
--没有q种语法Qselect ename,emp.deptno from emp left inner join dept on emp.deptno = dept.deptno;
--左外q接,从左往xQ匹配不上的记录也显CZ?
select ename,dept.deptno from emp left /*outer*/ join dept on emp.deptno = dept.deptno;
--叛_q接Q从叛_左找Q匹配不上的记录Q也昄一?
select ename,dept.deptno from emp right /*outer*/ join dept on emp.deptno = dept.deptno;
--没有叛_q接Qselect ename,dept.deptno from emp right inner join dept on emp.deptno = dept.deptno;
--全外q接
select ename,dept.deptno from emp full /*outer*/ join dept on emp.deptno = dept.deptno;
--左外,叛_的区?
--什么时候用外连接呢?比如领导向你要所有学生的列表,Z把所属的班也列出来,需要外q接
--在Where语句中用子查询
-----------------------------------------------------------------
--雇员中最高薪水的人员名称
--1,先求出最高薪?
--2,再求雇员中最高薪水的人员名称
select ename from emp where sal=(select max(sal) from emp)
--有哪些h的薪水是在整个雇员的q_薪水之上?
select ename,sal from emp where sal >(select avg(sal) from emp)
-----------------------------------------------------------------
--雇员中哪些h是经理h
--1,首先查询mgr中有哪些L
--2,再看有哪些h员的L在此出现
select distinct mgr from emp where mgr is not null order by mgr
select ename
from emp
where empno in (select distinct mgr from emp where mgr is not null )
--where in 中不让写orderby
select ename
from emp
where empno in (select distinct mgr from emp where mgr is not null order by mgr)
-----------------------------------------------------------------
--在From子句中用子查询
------------------------------------------------------------------
--部门q_薪水的等U?
--1,首先每个部门的q_薪水求出?
--2,然后把结果当成一张表Q再用这张结果表和salgrade表做q接Q以此求得薪水等U?
select deptno,avg(sal) from emp group by deptno
select * from (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade
on avg_sal between losal and hisal;
-----------------------------------------------------------------
--每个部门最高薪水的人员名称
--1,首先每个部门的最高薪水求出来
--2,然后把结果当成一张表Q再用emp和这张结果表做连接,以此求得每个部门最高薪水的人员名称
select deptno,max(sal) from emp where deptno is not null group by deptno
select ename from emp e join
(select deptno,max(sal) max_sal from emp where deptno is not null group by deptno ) t
on sal = max_sal and e.deptno = t.deptno
-----------------------------------------------------------------
--哪些人的薪水在部门的q_薪水之上
--1,首先每个部门的q_薪水求出?
--2,然后把结果当成一张表Q再用emp和这张结果表做连接,以此求得哪些人的薪水在部门的q_薪水之上
select deptno,avg(sal) avg_sal from emp group by deptno
select * from emp join (select deptno,avg(sal) avg_sal from emp group by deptno)t
on (sal>avg_sal and emp.deptno=t.deptno)
-----------------------------------------------------------------
--求部门中(所有h?q_的薪水等U?形式如:
-- deptno avg_grade
-- 10 3.67
-- 20 2.8
-- 30 2.5
--1,先求每个人的薪水{
--2,再按照部门分l?求^均数
select deptno,sal,grade from emp join salgrade on sal between losal and hisal
select deptno,avg(grade) from (select deptno,sal,grade from emp join salgrade on sal between losal and hisal)t group by deptno
------------------------------------------------------------------------------------------
--使用伪字D?rownumQ?---------------------
------------------------------------------------------------------------------------------
--用来标识每条记录的行P行号?开始,每次递增1
select rownum,emp.* from emp;
--oracle下rownum只能使用 < <=Q?不能使用 = > >= {比较操作符Q?
select rownum,emp.* from emp where rownum<5;
--当rownum和order by 一起用时Q会首先选出W合rownum条g的记录,然后再排?
--(错误的写?例如Q当我们要求薪水最高的?个hӞ最直接的想法可以这样写Q?
select * from emp where rownum<5 order by sal desc
--(正确的写?可以q样?
select * from
(select * from emp order by sal desc) t
where rownum<=5
--------------------------------------------------------
--不准用组函数(即MAX()),求薪水的最高?面试?
--W一U解军_?
--1,先把所有薪水按照倒序排列
--2,再取W一?
select * from
(select sal from emp order by sal desc) t
where rownum=1
--W二U解军_?
--1,先跨表查询自?先求出的l果?e1.sal不可能出现最大数
--2,然后再not in
select e2.sal from emp e1,emp e2 where e1.sal>e2.sal
select sal from emp where sal not in(select e2.sal from emp e1,emp e2 where e1.sal>e2.sal)
-----------------------------------------------------------------
--求^均薪水最高的部门的部门编?
--W一U解军_?
--1,先求出每个部门的q_薪水,
select deptno,avg(sal) avg_sal from emp group by deptno
--2,再求每个部门的^均薪水的最高?
select max(avg_sal) from (1111111111111111111111111)
--3,最后再求第一步结果中avg_sal = 最高薪水的记录.
select deptno from (111111111111) where avg_sal = (22222222)
select deptno
from (select deptno,avg(sal) avg_sal from emp group by deptno)
where avg_sal =
(select max(avg_sal)
from (select deptno,avg(sal) avg_sal from emp group by deptno))
--没法考虑q列W一的情?
select deptno from
(select deptno,avg(sal) avg_sal from emp group by deptno order by avg(sal) desc)
where rownum<=1
--W二U解军_?
--1,上面的W一步第二步合ƈ,先求最高^均薪?用max(avg(sal))的办?
--不能写成select deptno,max(avg(sal)) from emp group by deptno
select max(avg(sal)) from emp group by deptno
--2,求出每个部门的^均薪?
select deptno,avg(sal) avg_sal from emp group by deptno
--3,最后再求第二步l果?x个部门的q_薪水),avg_sal = (W一步结?的记?即avg_sal =最高薪水的记录.
select deptno from (select deptno,avg(sal) avg_sal from emp group by deptno)
where avg_sal =(select max(avg(sal)) from emp group by deptno)
--W三U解军_?
--1,先求出每个部门的q_薪水,
select avg(sal) avg_sal from emp group by deptno
--2,求最高^均薪?用max(avg(sal))的办?
select max(avg(sal)) from emp group by deptno
--3,再用having语句, avg(sal) = W二步的l果
注意Qؓl函数v的别名在having中不能用
select deptno from emp group by deptno
having avg(sal) = (select max(avg(sal)) from emp group by deptno)
-----------------------------------------------------------------
--求^均薪水最高的部门的部门名U?
--1,部门q_最高薪?
--2,得到部门~号列表,注意用group by deptno
--3,再应用having子句, having avg(sal) = (W一步的l果)
--4,得到q_最高薪水的那个部门的编?
--5,再得到部门名U?
select dname from dept where deptno in
(
select deptno
from (select deptno,avg(sal) avg_sal from emp group by deptno)
where avg_sal =
(select max(avg_sal)
from (select deptno,avg(sal) avg_sal from emp group by deptno))
)
-----------------------------------------------------------------
--求^均薪水的{最低的部门的部门名U?
--W一步:部门q_薪水的等U?分成两个步?W一步是求部门q_薪水
select * from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade on avg_sal between losal and hisal
--W二步:最低的{?
select min(grade) from (1111111111111111111111111)
--W三步:{于最低值的部门~号
------------有错?应该是grade=
select deptno from (111111111111) where grade = (22222222222222)
--W四步:求名U?
select dname from dept where deptno in(33333333333)
select dname
from dept
where deptno in
(
select deptno
from (select *
from (select deptno, avg(sal) avg_sal
from emp
group by deptno) t
join salgrade on avg_sal between losal and hisal)
where grade =
(select min(grade)
from (select *
from (select deptno, avg(sal) avg_sal
from emp
group by deptno) t
join salgrade on avg_sal between losal and hisal)))
--也可以用视图的方式来解决
--conn sys/bjsxt as sysdba
--grant create table, create view, create sequence to scott
--ҎW一步的l果,建立一个view
create or replace view v1 as
--必须明确定义?
select deptno, avg_sal, grade from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade on avg_sal between losal and hisal
--查看一?
select * from v1
--查询一?
--带入view
select dname from dept where deptno in
(select deptno from (v1) where grade = (select min(grade) from v1))
-------------------------------------------------------------
--Z么in的后面不能order by Q?
---------------------------------------------------------------
--求部门经理h中^均薪水最低的部门名称 (思考题)
W一?求部门经理的雇员~号
select distinct mgr from emp where mgr is not null
W二步,按部门统计,求部门经理的q_薪水
select deptno,avg(sal) avg_sal from emp where empno in (select distinct mgr from emp where mgr is not null)group by deptno
W三步,求最低?
select min(avg(sal)) from emp where empno in (select distinct mgr from emp where mgr is not null)group by deptno
W四步,求部门经理h中^均薪水最低的部门名称
select deptno from (2222222222222) where avg_sal =(333333333333333333333333)
select dname from dept where deptno in (select deptno from (select deptno,avg(sal) avg_sal from emp where empno in (select distinct mgr from emp where mgr is not null)group by deptno) where avg_sal =(select min(avg(sal)) from emp where empno in (select distinct mgr from emp where mgr is not null)group by deptno))
----------------------------------------------------------------------------
--求比普通员工的最高薪水还要高的经理h名称
--1,求所有经理的~号
create or replace view v1 as
select distinct mgr from emp where mgr is not null
select * from v1
--2,普通员工的最高薪?
select max(sal) from emp where empno not in (select distinct mgr from emp where mgr is not null)
--3,
select ename from emp where empno in (select * from v1)
and sal > (select max(sal) from emp where empno not in (select distinct mgr from emp where mgr is not null))
--?
select ename from emp where empno in (select distinct mgr from emp where mgr is not null)
and sal > (select max(sal) from emp where empno not in (select distinct mgr from emp where mgr is not null))
------------------------------------------------------------------------------
--求薪水最高的?名雇?
--1,先观察一?
--2,看看rownum的作?
--3,不是我们惌的结?
select ename,sal from emp where rownum<=5 order by sal desc
--4,先order by,再rownum
select * from
(select ename,sal from emp order by sal desc ) t
where rownum<=5
--------------------------------------------------------------------------------
--求薪水最高的W?到第10名雇?重点掌握)
--q种没法实现,oracle下rownum只能使用 < <=Q?不能使用 = > >= {比较操作符
--注意里面的rownum和外面的rownum的区别,外面要想讉K里面的rownumQ必d得一个别名?
select * from
(select ename,sal from emp order by sal desc ) t
where rownum>=5
and rownum<=10
--所以再套一层select
select * from
(select t.*,rownum r from
(select ename,sal from emp order by sal desc ) t
)
where r>=5
and r<=10
--q有一U排序方?
select * from
(select * from emp order by sal desc)where rownum<=10
minus
select * from
(select * from emp order by sal desc)where rownum<=5
--------------------------------------------------------------------
--l习: 求最后入职的5名员?
--1,每个人的入职旉
--2,取前5?
-----------------------------------------------------------------
--求每个部门中薪水最高的前两名雇?
--1,每个员工的姓名,部门Q工?按部门和工资(倒序)排列
select ename,deptno,sal from emp order by deptno,sal desc
--2,套一?加上个r
select ename,deptno,sal,rownum r from
(select ename,deptno,sal from emp order by deptno,sal desc) t
--3,创徏试图
create or replace view v1
as
select ename,deptno,sal,rownum r from
(select ename,deptno,sal from emp order by deptno,sal desc) t
--观察一?
select * from v1
--每个部门?薪水最高的W一?q创?
create or replace view v2 as
select deptno,min(r) min_r from v1 group by deptno
--两个view跨表q接,大于薪水最高的行数,于最高的行数+1,q且部门~号要匹?
select ename from v1 join v2
on ( v1.deptno = v2.deptno and v1.r >=v2.min_r and v1.r<=v2.min_r+1)
-------------------------------------------------------------------------------
--面试? 比较效率
select * from emp where deptno = 10 and ename like '%A%';
select * from emp where ename like '%A%' and deptno = 10;
---------------------------------------------------------
--使用union、minus
--使用union、minus可以用来实现l果集的合ƈ和去除(可以理解为加和减Q,例如Q?
select * from emp where deptno=10
union
select * from emp where deptno=20;
--相当?
select * from emp where deptno=10 or deptno=20
--而下面的语句
select * from emp where deptno in (10,20)
minus
select * from emp where sal < 1500;
--相当?
select * from emp where deptno in(10,20) and sal>=1500
--求分D|C水的个数
如:
scale total
<800 0
801-1000 2
1001-2000 3
2001-5000 6
>5000 8
select '<800' as scale ,count(*) as total from emp where sal<800
union
select '<801-1000' as scale ,count(*) as total from emp where sal<=1000 and sal>=801
--或者显C成?
--注意Q用between .. and .. 的时候,包含了最大和最倹{?
800-1000 1001-2000 2001-5000
2 3 6
select * from
(select count(*) as "800-1000" from emp where sal >=800 and sal <= 1000),
(select count(*) as "1001-2000" from emp where sal >=1001 and sal <= 2000),
(select count(*) as "2001-5000" from emp where sal >=2001 and sal <= 5000)
--或显C成?
DEPTNO 800-2000 2001-5000
------ ---------- ----------
30 5 1
20 2 3
10 1 2
select t.deptno,"800-2000","2001-5000" from
(select deptno,count(*) as "800-2000" from emp where sal between 800 and 2000 group by deptno) t
join
(select deptno,count(*) as "2001-5000" from emp where sal between 2001 and 5000 group by deptno) t1
on t.deptno = t1.deptno
-----------------------------------------------------------------------------------
--每个薪水{有多名雇员 Q?
--1,先求出每个雇员的薪水{
--2,再group一?
<ResourceParams name="jdbc/mldn">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/mldn</value>
</parameter>
</ResourceParams>
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
username="root"
password="root"
url="jdbc:mysql://localhost:3306/mldn"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="30"
maxWait="5000"
maxActive="100"/>
<Resource name="jdbc/mldn" auth="Container"
type="javax.sql.DataSource" username="root" password="xtcwvt"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mldn"
maxActive="8" maxIdle="4"/>
W二步:配置应用目录下的web.xml文gQ?font face="Verdana">?lt;web-app></web-app>之间加入以下代码Q?
<description>DB Connection</description>
<res-ref-name>jdbc/mldn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2、配|web.xml
3、JNDI使用