1.数据基础表:
2.简单增删改语句:
insert into t_user(id,username,password,roleid) values(seq_user.nextval,'wind','520',109);
delete from t_user t where t.roleid is null;
update t_user t set t.username='saturn',t.password='456' where t.id=106;
3.查询语句:
1) count|distinct
select count(*) from t_user t; //统计表的记录数
select distinct(t.username) from t_user t; //返回不重复的username
select count(distinct t.username||t.password) from t_user t; //统计表不重复username和password的记录数
select t.username,count(t.username) from t_user t group by t.username having count(t.username)>1 //统计重复username的:username及其重复次数
2)inner join | outer left join | outer right join
1 | A |
2 | B |
3 | C |
t_t2
ColumnC | ColumnD |
2 | x |
4 | Y |
a)inner join
columnA | columnB | columnC | columnD |
2 | B | 2 | X |
b)left outer join
columnA | columnB | columnC | columnD |
1 | A | ||
2 | B | 2 | X |
3 | C |
c)right outer join
columnA | columnB | columnC | columnD |
2 | B | 2 | X |
4 | Y |
d)连接三张表:
Select * from (tableA ainner join tableB b on a.id=b.id) inner join tableC c on c.id=tableA.id
3)oracle 分页查询:
select * from (select rownum,t.* from t_user t) where rownum>=1 and rownum<=5;
select * from (select rownum rn,t.* from t_user t order by t.id desc) where rn between 4 and 20;
4)多表查询:
select t.* from t_user t,t_role t1 where t.roleid=t1.id and t1.rolename='管理员';
5)oracle时间的处理:
默认设置时间类型为:date 采用函数 to_date('2010-09-07','yyyy-mm-dd'),或 to_date('2010-9-5 10:00:00','yyyy-mm-dd HH24:MI:SS')
insert into t_log t values(hibernate_sequence.nextval,104,'新建用户114', to_date('2010-9-5 10:56:47','yyyy-mm-dd HH24:MI:SS')); select t.* from t_log t where cdate between to_date('2010-09-05','yyyy-mm-dd') and to_date('2010-09-07','yyyy-mm-dd')
6)top N等问题:
select t.* from t_user t where rownum<=3 order by t.id ; //前三最小值 select t.* from t_user t where rownum<=3 order by t.id desc; //前三最大值 select avg(t.roleid),sum(t.id) from t_user t //平均值与统计 select t.* from t_log t where t.userid<(select avg(id) from t_user); // select t.username,avg(t.roleid) from t_user t group by t.username ; //统计表中某相同字段username的roleid平均值 select min(t.username),max(t.password) from t_user t //最大值与最小值
7)字符函数:
select upper(substr(username,1,1))||lower(substr(username,2,length(username)-1)) from t_user //将t_user表中的username字段内容的第一个字母大写显示,之后的小写显示
8)相关函数
select mod(10,3) from dual ---1 select mod(4.1,3.1) from dual ---1 select mod(4.9,3.1) from dual ---1.8 select ceil(10/3) from dual ---4 select ceil(3.1) from dual ---4 select floor(3.1) from dual ---3 select floor(3.9) from dual ---3 select round(3.5) from dual ---4 select round(3.1) from dual ---3
sql:select s.cname,count(s.cname),sum(s.CONTAIN) from (select * from TC c,TR r,TCR cr where cr.cid=c.cid and cr.roomid=r.roomid) s group by s.cname;
1) select distinct(t1.eid),t1.btime,t1.etime from t_exam t1, t_exam t2 where ((t1.btime between t2.btime and t2.etime) 2 or (t1.etime between t2.btime and t2.etime)) and t1.eid!=t2.eid order by t1.eid; 2) select distinct(t1.eid),t1.btime,t1.etime from t_exam t1, t_exam t2 where (t1.btime>t2.btime and t1.btime<t2.etime) or (t1.etime>t2.btime and t1.etime<t2.etime) or (t1.btime in (select t.btime from t_exam t group by t.btime having count(t.btime)>1)) order by t1.eid;