SQL常用语句

来源:互联网 发布:html5商城源码 编辑:程序博客网 时间:2024/06/05 07:44
 1,查询某个时间段的记录
select * from 表名 where create_time >= to_date('2012-01-02','yyyy-mm-dd') and create_time <= to_date('2012-01-08','yyyy-mm-dd') or update_time  >= to_date('2012-01-02','yyyy-mm-dd') and update_time  <= to_date('2012-01-08','yyyy-mm-dd')

查询2012年1月2日到2012年1月8日之间,create_time 和update_time新增和更新的记录


分组

select t.function_name from function_setting_info t group by t.function_name;

去重

select distinct t.function_name from function_setting_info t

排序

select * from function_setting_info t where t.function_name='fun_ZJ_show' order by t.department_code ;


2,模糊查询
select * from 表名 where 字段名 like  '%abc%';
%是通配符

3,把多个表中相同字段的值累加

select sum(visit_count) from (

select visit_count from s_dns_20120301

where region_code='430400'

union all

select visit_count from s_dns_20120302

where region_code='430400'

union all

select visit_count from s_dns_20120303

where region_code='430400'

)

4,一个表中同一个字段下相同内容中取当天时间的记录

select * from (

select top_domain_name_url,min(create_time) create from dns_all

order by top_domain_name

                         )dns,

                        in_top_domain_name  top

where dns.create=to_date('2012-03-05','yyyy-mm-dd')

dns.top_domain_name_url=top.url

 

5,创建表
create table 表名 (字段名 字段类型 是否为空);
create table TEST (id int not null primary key,
      number int(20) not null);

6,删除表
drop table 表名;

7,删除表中所有内容
delete from 表名;
delete from TEST;

8,在表中增加字段
alter table test1 add class char(10);
test1表名,class新增字段名,char(10)新增字段的类型

9,在表中删除字段
alter table test1 drop column class;
test1表名,class删除字段名,column为关键字

10,在表中插入记录
insert into test1 values('1001','张三','一班');

11,在表中删除符合条件的记录
delete from test1 where id = '1001';

12,把从一张表的查询结果插入到另一张表中
insert into test3 (person,total)
select num,sum(chenji) from test2
group by num ;

insert A表(列1,列2。。。)
select 列1,列2。。。) from B表

13,无参数的存储过程
create or replace procedure per_chenji
is
begin
insert into test3 (person,total)
select num,sum(chenji) from test2
group by num ;
commit;
end per_chenji;

有参数的存储过程

create orreplaceprocedure xs_proc(temp_nameinvarchar2,
temp_num out
number)is
num_1
number;
num_2
number;
begin
select yu_wen, shu_xue
into num_1, num_2
from xuesheng
where xing_ming= temp_name;
--dbms_output.put_line(num_1 + num_2);
temp_num := num_1+ num_2;
end;

 


14,oracle定时器简单实例

declare
jobno number;
begin
dbms_job.submit(
               jobno,
               'PER_CHENJI;',
               sysdate,
               'sysdate + 1 + 2 / (24)'
               );
commit;
end;

jobno是job的变量名
PER_CHENJI是存储过程名称
sysdate是自动任务第一次执行的时间,如果需要它立即执行,则使用sysdate
sysdate + 1 + 2 / (24)每天的凌晨2点执行

 15,修改或改变符合条件的记录的名称或数值

update 表名 set name='李四' where name ='张三

'update 表名 set score=score+10 where score>=70

 

16,表A有no,name,age,表B有no,kemu,score。查询每个学生的no,name,score

select a.no,a.name,b.num (

select no,sum(score) num  from B group by no

                                                 )b,A a

where a,no=b.no

用内连接的方法与上述的SQL等效

select t1.num,t1.name,t1.class,t2.score from
(select num,sum(chenji) score from test2 group by num) t2
inner join test1 t1 on t1.num=t2.num

 

17,inner join嵌套查询

select t1.num,t1.name,t2.chenji from test1 t1 inner join test2 t2 on
t1.num = t2.num

普通select 查询

select t1.num,t1.name,t2.chenji from test1 t1, test2 t2
where t1.num=t2.num

两者等效

 

18,左连接,右连接查询

表A中表ID,NAME,表B中有ID,AGE

表A的内容:                     表B的内容:

101  李四                           101       30

102  张三                           102       25

103  王五                           104        45

左连接查询:

select a.id,a.name,b.age from a left join b on a.id=b.id

以表A的记录为准,查询后的内容为:

101  李四    30

102  张三     25 

103  王五      空

右连接与左连接的原理相反, 根据上例以表B的记录为准,这里不再重述

非限制连接(CROSS JOIN)
非限制连接(CROSS JOIN),就是指不带WHERE子句的查询。在数学上,就是表的笛卡尔积。若R表和S表非限制连接,而且R表有X行,S表有Y行,那么结果集是X * Y行

外连接:

  外连接(也就是左连接和右连接)除了显示匹配相等连接条件的数据外,还可以显示某一个表中无法匹配相等连接条件的记录

group by,order by用法:

有这样一个表id  科目   姓名   分数1  英语   王生   982  语文  张生   833  数学  王生   914  英语  张生   955  化学  李生   85
1. 只能用一句sql语句,查询到科目成绩总和最高的那位学生2. 只能用一句sql语句,查询到各科成绩都大于90的那个学生
1. select top 1 姓名,sum(分数) as 分数总和 from 表名 group by 姓名  order by 分数总和 desc2. select 姓名 from 表名 group by 姓名 having min(分数)>90