经典sql语句
本文章将详细介绍oracle数据操作中的一些详细的sql
基本sql字符函数数学函数日期时间处理函数聚合函数
基础sql
以下主要列出来常用的sql
create table table_name(
id varchar2(300) primary key,
name varchar2(200) not null
);
insert into table_name (id,name) values ('aa','bb');
update table_name set id = 'bb' where id='cc';
delete from table_name where id ='cc';
drop table table_name;
alter table table_name rename to table_name_1;
insert into table1 (select * from table2);
create table table1 select * from table2 where 1>1;
create table table1 select * from table2;
create table table1 as select id, name from table2 where 1>1;
select id,name (
case gender when 0 then '男'
when 1 then '女' end ) gender
from table1
字符函数
select substr(
'abcdefg',
1,
5)substr,
instr(
'abcdefg',
'bc') instr,
'Hello'||
'World' concat,
trim(
' wish ') trim,
rtrim(
'wish ') rtrim,
ltrim(
' wish') ltrim,
trim(leading
'w' from
'wish') deleteprefix,
trim(trailing
'h' from
'wish') deletetrailing,
trim(
'w' from
'wish') trim1,
ascii(
'A') A1,
ascii(
'a') A2,
chr(
65) C1,
chr(
97) C2,
length(
'abcdefg') len,
lower(
'WISH')lower,
upper(
'wish')upper,
initcap(
'wish')initcap,
replace(
'wish1',
'1',
'youhappy') replace,
translate(
'wish1',
'1',
'y')translate,
translate(
'wish1',
'sh1',
'hy')translate1,
concat(
'11',
'22') concat
from dual;
select to_number(
'0123')number1,
trunc(to_number(
'0123.123'),
2) number2,
to_number(
'120.11',
'999.99') number3,
to_number(
'0a',
'xx') number4,
to_number(
100000,
'xxxxxx') number5
from dual;
数学函数
select abs(-2) value from dual;
select ceil(-2.001) value from dual;
select floor(-2.001) value from dual;
select trunc(-2.001) value from dual;
select round(1.234564,4) value from dual;
select power(4,2) value from dual;
select sqrt(16) value from dual;
select dbms_random.value() from dual; (默认是0到1之间)
select dbms_random.value(2,4) value from dual; (2-4之间随机数)
select sign(-3) value from dual;
select sign(3) value from dual;
select greatest(-1,3,5,7,9) value from dual;
select least(-1,3,5,7,9) value from dual;
select nvl(null,10) value from dual;
select nvl(score,10) score from student;
日期函数
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')currenttime,
to_char(sysdate,'yyyy') year,
to_char(sysdate,'mm') month,
to_char(sysdate,'dd') day,
to_char(sysdate,'day') week,
to_char(sysdate,'hh24')hour,
to_char(sysdate,'mi') minute,
to_char(sysdate,'ss') second
from dual;
select to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss')currenttime,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'yyyy')year,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mm')month,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'dd') day,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day') week,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day','NLS_DATE_LANGUAGE=American') week, --设置语言
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'hh24')hour,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mi') minute,
to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'ss') second
from dual;
select months_between(to_date('03-31-2014','MM-DD-YYYY'),to_date('12-31-2013','MM-DD-YYYY')) "MONTHS"
FROM DUAL;
select sysdate today, next_day(sysdate,6) nextweek from dual;
select cardid, borrowdate from borrow where to_date(borrowdate,'yyyy-mm-dd hh24:mi:ss')
between
to_date('2014-02-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and
to_date('2014-05-01 00:00:00','yyyy-mm-dd hh24:mi:ss');
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') currenttime,
to_char(sysdate - interval '7' year,'yyyy-mm-dd hh24:mi:ss') intervalyear,
to_char(sysdate - interval '7' month,'yyyy-mm-dd hh24:mi:ss') intervalMonth,
to_char(sysdate - interval '7' day,'yyyy-mm-dd hh24:mi:ss') intervalday,
to_char(sysdate - interval '7' hour,'yyyy-mm-dd hh24:mi:ss') intervalHour,
to_char(sysdate - interval '7' minute,'yyyy-mm-dd hh24:mi:ss') intervalMinute,
to_char(sysdate - interval '7' second,'yyyy-mm-dd hh24:mi:ss') intervalSecond
from dual;
select add_months(sysdate,12) newtime from dual;
select extract(month from sysdate) "This Month",
extract(year from add_months(sysdate,36)) " Years" from dual;
聚合函数
select count(1) as count from student;
select count(*) as count from student;
select count(distinct score) from student;
select avg(score) score from student;
select avg(distinct score) from student;
select classno,avg(score) score from student group by classno;
select max(score) from student;
select classno, max(score) score from student group by classno;
select min(score) from student;
select classno, min(score) score from student group by classno;
select stddev(score) from student;
select classno, stddev(score) score from student group by classno;
select sum(score) from student;
select classno, sum(score) score from student group by classno;
select median(score) from student;
select classno, median(score) score from student group by classno;
伪列rownum
select * from student where rownum <3;
select * from(
select rownum rn ,id,name from student
)
where rn>2;
select rownum rn, student.* from student
where rn >3;
select * from (
select rownum rn, student.* from student)
where rn >3 and rn<6;
select * from (
select rownum rn, t.* from (
select d.* from DJDRUVER d order by drivernumber)t
)p where p.rn<10;
select * from (
select rownum rn, t.* from (
select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
)t
)p where p.rn<9 and p.rn>6;
select * from (
select rownum rn, t.* from (
select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
where rownum<9
where p.rn>6;
分页查询
select * from (
select rownum rn, d.* from DJDRIVER d )p
where p.rn<=20 and p.rn>=10;
select * from (
select rownum rn, d.* from DJDRIVER d )p
where p.rn between 10 and 20;
select * from (
select rownum rn, d.* from DJDRIVER d where rownum<=20 )p
where p.rn>=10;
select * from (
select rownum rn, t.* from (
select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
)t
)p
where p.rn<=20 and p.rn>=10;
select * from (
select rownum rn, t.* from (
)t
)p
where p.rn between 10 and 20;
select * from (
select rownum rn, t.* from (
select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
where rownum<=20
)p
where p.rn>=10;