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;
基本上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;
相关文章推荐
- SQL基础操作_3_数据字典
- SQL数据操作基础(中级)8
- SQL数据操作基础(初级) 1
- SQL数据操作基础(中级) 9
- Hadoop Hive基础SQL语法(DQL 操作:数据查询SQL)
- SQL数据操作基础(中级)7
- SQL数据操作基础(初级) 2
- SQL数据操作基础(中级) 10
- SQL基础(2):数据操作-增删改
- SQL数据操作基础(中级)6
- SQL数据操作基础(初级) 3
- SQL操作数据——SQL组成,查询基础语法,where,Oracle常用函数等
- SQL数据操作基础(中级)
- SQL 存储过程的使用对数据的操作(整理思绪补基础)
- 数据应用达人之SQL基础教程分享9-数据操作
- SQL数据操作基础(初级)5
- SQL数据操作基础(初级) 4
- SQL数据操作基础(初级)4
- SQL数据操作基础(初级) 5
- [SQL]SQL语言入门级教材_SQL数据操作基础(二)