我的数据是这样:
字段1 字段2 字段3 字段4 字段5
1 a a 1 2005
2 a a 2 2006
3 a a 4 2007
4 a b 1 2004
5 b a 1 2005
6 a c 2 2008要做的工作就是把数据库里的字段2,字段3对应确定的记录字段4从新排序!可以看到aa这里是124少了个3 我就是要把这里的124改成123,还有6ac这里是2改成1!
这里sql怎么写啊?
字段1 字段2 字段3 字段4 字段5
1 a a 1 2005
2 a a 2 2006
3 a a 4 2007
4 a b 1 2004
5 b a 1 2005
6 a c 2 2008要做的工作就是把数据库里的字段2,字段3对应确定的记录字段4从新排序!可以看到aa这里是124少了个3 我就是要把这里的124改成123,还有6ac这里是2改成1!
这里sql怎么写啊?
create table tab
(
c1 int,
c2 varchar(10),
c3 varchar(10),
c4 int,
c5 varchar(10),
)insert into tab select 1,'a','a',1,'2005'
insert into tab select 2,'a','a',2,'2006'
insert into tab select 3,'a','a',4,'2007'
insert into tab select 4,'a','b',1,'2004'
insert into tab select 5,'b','a',1,'2005'
insert into tab select 6,'a','c',2,'2008'--更新语句
update t1
set t1.c4 = t2.xh
from
tab t1,
(select c1,xh = (select count(1) from tab where c2 = a.c2 and c3 = a.c3 and c1 <= a.c1)
from tab a
)t2
where t1.c1 = t2.c1--结果
1 a a 1 2005
2 a a 2 2006
3 a a 3 2007
4 a b 1 2004
5 b a 1 2005
6 a c 1 2008
xh是t2中的排序字段。
做了个自连接
真正的用途在于如果表进行自连接的话
比方说我要一个查询select * from tab t1 ,tab t2 where ...
tab其实就是一个表,但是这里给了他两个别名,那就当两个表使了xh是一个新列,里面放的是查询出来的结果 这里的t2 跟上面的不太一样
意思是把t2前面括号里的查询语句的结果当作一个表来用,把这个表的名字命为t2
我的表名是"SC"."MOLAR_DOCU"
字段分别是:SEQ_NO,ORDERS_NO,ORDERS_SEQ,MOLAR_DOCU能不能再帮忙理一下?单位其他人都闪了,就留偶一个菜鸟在....
set 字段4 = (select count(1) from 表名称 where 字段2 = a.字段2 and 字段3 = a.字段3 and 字段1 < a.字段1) + 1
from 表名称 a
set 字段4 = (select count(1) from 表名称 where 字段2 = a.字段2 and 字段3 = a.字段3 and 字段1 < a.字段1) + 1
from 表名称 a
set "MOLAR_DOCU"= (select count(1) from "SC"."MOLAR_DOCU" where "ORDERS_NO" = a."ORDERS_NO" and "ORDERS_SEQ" = a."ORDERS_SEQ" and "SEQ_NO" < a."SEQ_NO") + 1
from "SC"."MOLAR_DOCU" a ;我全对应的写上去了!在pb6.5里!怎么老是说我的sql没有正常结束?
if object_id('表名称') is not null drop table 表名称
select 1 as 字段1, 'a' as 字段2, 'a' as 字段3, 1 as 字段4, '2005' as 字段5
into 表名称
union select 2, 'a', 'a', 2, 2006
union select 3, 'a', 'a', 4, 2007
union select 4, 'a', 'b', 1, 2004
union select 5, 'b', 'a', 1, 2005
union select 6, 'a', 'c', 2, 2008
------------------------------------------
update a
set 字段4 = (select count(1) from 表名称 where 字段2 = a.字段2 and 字段3 = a.字段3 and 字段1 < a.字段1) + 1
from 表名称 a
select * from 表名称
/*
字段1 字段2 字段3 字段4 字段5
1 a a 1 2005
2 a a 2 2006
3 a a 3 2007
4 a b 1 2004
5 b a 1 2005
6 a c 1 2008
*/
------------------------------------------
drop table 表名称