SQL基础(七)数据操作

数据操作 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博客 ” ,链接:http://blog.itpub.net/174388/viewspace-905977/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/174388/viewspace-905977/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值