ITPub博客

首页 > 数据库 > Oracle > SQL基础(七)数据操作

SQL基础(七)数据操作

原创 Oracle 作者:cxck 时间:2007-03-20 14:37:25 0 删除 编辑
数据操作 insert,update,where,having,distinct[@more@]

基本上Oracle、SQL-Server、MDB可以通用。
■追加纪录
/* 基础篇 */
insert into cxck.t订货 (订货号, 产品号, ...)
values ('000001', 'a001', ...);

/* 子查询追加 */
insert into cxck.t订货1
select * from cxck.t订货 where 产品号 = 'a001';

■修改纪录
/* 基础篇 */
update cxck.t订货 set 产品号 = 'a001', 订货数 = 100
where 订货号 = '000001';

/* 子查询修改 */
update cxck.t订货 set
产品号 = select 产品号 from ...
where 订货号 = '000001';

/* 复数列子查询修改 */
update cxck.t订货 set
(产品号, 订货数) = (select 产品号, 订货数 from ...)
where 订货号 = '000001';

■查询纪录
/* 基础篇 */
select * from cxck.t订货
where 产品号 = 'a001'
order by 订货号;

/* 条件查询 */
select * from cxck.t订货
where 订货数 between 100 and 200
order by 订货号;

/* 聚集运算结果的条件查询 */
select 客户号, 产品号, sum(订货数) as 订货合计 from cxck.t订货
where 客户号 = '1000' or 客户号 = '2000'
group by 客户号, 产品号
having sum(订货数) > 300
order by 客户号, 订货数;

/* 比较运算的条件查询(%) */
select * from cxck.t订货
where 订货号 like '1%'
order by 订货号;

/* 比较运算的条件查询(_) */
select * from cxck.t订货
where 订货号 like '1_____-001'
order by 订货号;

/* 去掉重复纪录 */
select distinct 产品号 from cxck.t订货 order by 产品号;

/* 统计记录数 */
select count(*) from cxck.t订货;

■复合查询
/* 相等子查询复合查询 */
select * from cxck.t订货
where 产品号 in
(select distinct 产品号 from cxck.t产品 where 产品号 = 'a001');

/* 不等子查询复合查询 */
select * from cxck.t订货
where 产品号 not in
(select distinct 产品号 from cxck.t产品 where 产品号 = 'a001');

/* 大于任意子查询复合查询 */
select * from cxck.t订货
where 产品号 > any
(select distinct 产品号 from cxck.t产品 where 产品号 = 'a001');

/* 大于全部子查询复合查询 */
select * from cxck.t订货
where 产品号 > all
(select distinct 产品号 from cxck.t产品 where 产品号 = 'a001');

/* 子查询结果复合查询 */
select * from
(select * from cxck.t订货 where 产品号 = 'a001')
where 订货数 > 100;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/174388/viewspace-905977/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    38
  • 访问量
    74700