更新中的顺序号 [英] Sequential Number in an Update
问题描述
这是对这里常见问题的一个小改动。我/ b
有一个包含sortorder的表格。用户可以在列中显示
指定显示的记录的任意顺序。用户
有时会按照我们用来编号的方式排序记录
BASIC计划(10,20,30等)。我想做一个更新查询并修复
这样每个记录都按顺序排列。我在这个新闻组中找到了一个例子
如何做到这一点。
但是,我有一个小问题!有时,用户会复制
分拣机。所以例如,我可能有两个记录是
sortorder是20.我在这个新闻组中找到的查询在
中不起作用。这是一些代码,以便你可以看到我的意思。
创建表格sorttest(
label char(5),
sortorder int
)
go
insert sorttest values(''joe'',20)
insert sorttest值(''dan'',10)
插入sorttest值(''jim'',44)
插入sorttest值(''tom'',20 )
插入sorttest值(''jan'',50)
- 数据转储
选择标签,排序顺序来自按排序顺序排序测试
- 我想修复所有的排序字段,以便它们是
顺序
update sorttest
set sortorder =(
select count(*)
来自sorttest子查询
其中sorttest。 sortorder< = subquery.sortorder
)
- 请注意tom和joe有两个SORTORDER = 4
选择标签,sortorder从sortorder顺序排序
博士op table sorttest
提前感谢您的帮助。
您需要其他标准才能进行独特排序。
试试这个
更新sorttest
set sortorder =(
select count(*)
来自sorttest子查询的
其中sorttest.sortorder< subquery.sortorder
或(sorttest.sortorder = subquery.sortorder
和sorttest.label< = subquery.label)
)
如果是一次更新,您可以执行此操作。所有行中的sortcolumns值
将被更改
DECLARE @sortcol INT
SET @sortcol = 0 - 如果你愿意,可以设置为任何其他值
UPDATE sorttest
SET @sortcol = sorcol = @sortcol + 1
MA Srinivas
er******* @ gmail.com 写道:
< blockquote class =post_quotes>
这是对这里常见问题的一个小改动。我/ b
有一个包含sortorder的表格。用户可以在列中显示
指定显示的记录的任意顺序。用户
有时会按照我们用来编号的方式排序记录
BASIC计划(10,20,30等)。我想做一个更新查询并修复
这样每个记录都按顺序排列。我在这个新闻组中找到了一个例子
如何做到这一点。
但是,我有一个小问题!有时,用户会复制
分拣机。所以例如,我可能有两个记录是
sortorder是20.我在这个新闻组中找到的查询在
中不起作用。这是一些代码,以便你可以看到我的意思。
创建表格sorttest(
label char(5),
sortorder int
)
go
insert sorttest values(''joe'',20)
insert sorttest值(''dan'',10)
插入sorttest值(''jim'',44)
插入sorttest值(''tom'',20 )
插入sorttest值(''jan'',50)
- 数据转储
选择标签,排序顺序来自按排序顺序排序测试
- 我想修复所有的排序字段,以便它们是
顺序
update sorttest
set sortorder =(
select count(*)
来自sorttest子查询
其中sorttest。 sortorder< = subquery.sortorder
)
- 请注意tom和joe有两个SORTORDER = 4
选择标签,sortorder从sortorder顺序排序
博士操作表sorttest
提前感谢您的帮助。
>有一个包含sortorder的表格。用户可以指定要显示的记录的任意顺序的列。<<
在RDBMS中混合显示和数据的经典错误?
>用户有时按照我们在BASIC程序(10,20,30等)中对行进行编号的方式排序记录[原文如此]。我想做一个更新查询并修复它,以便每个记录[原文如此]按顺序排列。 <<
行和记录是完全不同的概念。你仍然想着一个文件系统,而不是RDBMS。行号是纸质表格或输入屏幕的物理
表示,而不是逻辑数据
模型元素。
< blockquote class =post_quotes>
>但是,我有一个小问题!有时,用户复制了排序程序。 <<
Gee,你认为这可能是因为它们不是关系的b
验证或验证?并且
事实上你对这个愚蠢的列没有UNIQUE约束????
>提前感谢您的帮助。 <<
您可能想阅读有关RDBMS的书籍,数据建模和分层
架构
this is a slight change to a fequently asked question around here. I
have a table which contains a "sortorder" column where a user can
specify some arbitrary order for records to be displayed in. Users
have sometimes ordered records the way we used to number lines in a
BASIC program (10,20,30, etc.). I''d like to do an update query and fix
this so that every record is in sequential order. I found an example
in this newsgroup of how to do this.
However, I have a slight problem! Sometimes, the users duplicated the
sortorders. So for example, I might have two records were the
sortorder is 20. The query I found in this newsgroup does not work in
that case. Here is some code so that you can see what I mean.
create table sorttest (
label char(5),
sortorder int
)
go
insert sorttest values (''joe'',20)
insert sorttest values (''dan'',10)
insert sorttest values (''jim'',44)
insert sorttest values (''tom'',20)
insert sorttest values (''jan'',50)
-- data dump
select label, sortorder from sorttest order by sortorder
-- I''d like to fix all of the sortorder fields so that they are
sequential
update sorttest
set sortorder = (
select count(*)
from sorttest subquery
where sorttest.sortorder <= subquery.sortorder
)
-- note that tom and joe BOTH HAVE SORTORDER = 4
select label, sortorder from sorttest order by sortorder
drop table sorttest
Thanks in advance for any help.
You need additional criteria to sort uniquely.
Try this
update sorttest
set sortorder = (
select count(*)
from sorttest subquery
where sorttest.sortorder < subquery.sortorder
or (sorttest.sortorder = subquery.sortorder
and sorttest.label <= subquery.label)
)
If it is one tine update you can do this . Note values of sortcolumns
in all the rows will be changed
DECLARE @sortcol INT
SET @sortcol = 0 -- You can set to any other value if you like
UPDATE sorttest
SET @sortcol = sorcol = @sortcol + 1
M A Srinivas
er*******@gmail.com wrote:this is a slight change to a fequently asked question around here. I
have a table which contains a "sortorder" column where a user can
specify some arbitrary order for records to be displayed in. Users
have sometimes ordered records the way we used to number lines in a
BASIC program (10,20,30, etc.). I''d like to do an update query and fix
this so that every record is in sequential order. I found an example
in this newsgroup of how to do this.
However, I have a slight problem! Sometimes, the users duplicated the
sortorders. So for example, I might have two records were the
sortorder is 20. The query I found in this newsgroup does not work in
that case. Here is some code so that you can see what I mean.
create table sorttest (
label char(5),
sortorder int
)
go
insert sorttest values (''joe'',20)
insert sorttest values (''dan'',10)
insert sorttest values (''jim'',44)
insert sorttest values (''tom'',20)
insert sorttest values (''jan'',50)
-- data dump
select label, sortorder from sorttest order by sortorder
-- I''d like to fix all of the sortorder fields so that they are
sequential
update sorttest
set sortorder = (
select count(*)
from sorttest subquery
where sorttest.sortorder <= subquery.sortorder
)
-- note that tom and joe BOTH HAVE SORTORDER = 4
select label, sortorder from sorttest order by sortorder
drop table sorttest
Thanks in advance for any help.
>have a table which contains a "sortorder" column where a user can specify some arbitrary order for records to be displayed in. <<
The classic error of mixing display and data in the RDBMS?
>Users have sometimes ordered records [sic] the way we used to number lines in a BASIC program (10, 20, 30, etc.). I''d like to do an update query and fix this so that every record [sic] is in sequential order. <<
Rows and records are totally diffreent concepts. You are still
thinking about a file system, not RDBMS. Line numbers are a physical
representation of a paper form or input screen, not a logical data
model element.
>However, I have a slight problem! Sometimes, the users duplicated the sortorders. <<
Gee, do you suppose that might be due to the fact that they are not
relational, have no rules for validation or verifiication? And the
fact that you do not have a UNIQUE constraint on this silly column????
>Thanks in advance for any help. <<
You might want to read book on RDBMS, data modeling and tiered
architectures
这篇关于更新中的顺序号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!