您的位置:首页 > 数据库

SQL基础(七)操作数据

2007-04-12 16:50 417 查看
操作数据 insert、update、where、having、distinct

基本上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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: