帮助数据库更新的逻辑流程

发布于 2024-12-02 11:04:39 字数 313 浏览 0 评论 0原文

我有一个包含以下字段的数据库表...

key,
db_name,
string_ID_in_DB,
text1,
text2

text1 充满重复项,我想显示一次,因为用户需要根据 text1 内容填充 text2。

然后我需要更新每个 db_name 数据库中的 text2。如果我只显示非重复的 text1 字段,我会从显示中消除 20,000 条记录,但我需要弄清楚如何跟踪这些记录,以便我可以在相应的 db_name 数据库中更新它们。

任何有关如何执行此操作的建议将不胜感激。

谢谢,

I have a database table with the following fields...

key,
db_name,
string_ID_in_DB,
text1,
text2

text1 is full of duplicates and I would like to show it once, as the users need to populate text2 based on text1 content.

I then need to update text2 in each db_name database. If I only display the non duplicate text1 fields I eliminate 20,000 records from display, but I need to figure out how to keep track of these so that I can then update them in the corresponding db_name databases.

Any suggestions on how to do this would be greatly appreciated.

thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-12-09 11:04:39

我真的不知道你在追求什么,这个问题不是很清楚。另外,并非所有 SQL 都是相同的(OP 没有说明它们的用途),因此这里有一种在 SQL Server 中执行此操作的方法:

DECLARE @YourTable table (KeyID int, text1 varchar(10))
INSERT @YourTable VALUES (1,'aaa')
INSERT @YourTable VALUES (2,'bbb')
INSERT @YourTable VALUES (3,'aaa')
INSERT @YourTable VALUES (4,'ccc')
INSERT @YourTable VALUES (5,'ccc')
INSERT @YourTable VALUES (6,'ccc')
INSERT @YourTable VALUES (7,'ddd')
INSERT @YourTable VALUES (8,'aaa')

--just display the first occurrence rows of each text1 value
SELECT
    *
    FROM (SELECT KeyID,text1,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber FROM @YourTable) dt
    WHERE RowNumber=1

--delete all duplicates
;WITH NumberedRows AS 
(SELECT
     KeyID,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber
     FROM @YourTable
)
DELETE NumberedRows
    WHERE RowNumber>1

select * from @YourTable  --<<display remaining rows

输出:

KeyID       text1
----------- ----------  <<from 1st select
1           aaa
2           bbb
4           ccc
7           ddd

(4 row(s) affected)


KeyID       text1
----------- ----------  <<from second select after delete
1           aaa
2           bbb
4           ccc
7           ddd

(4 row(s) affected)

I'm really not sure what you are after, the question is not very clear. Also, not all SQL is the same (OP doesn't say what their using), so here is a way to do it in SQL Server:

DECLARE @YourTable table (KeyID int, text1 varchar(10))
INSERT @YourTable VALUES (1,'aaa')
INSERT @YourTable VALUES (2,'bbb')
INSERT @YourTable VALUES (3,'aaa')
INSERT @YourTable VALUES (4,'ccc')
INSERT @YourTable VALUES (5,'ccc')
INSERT @YourTable VALUES (6,'ccc')
INSERT @YourTable VALUES (7,'ddd')
INSERT @YourTable VALUES (8,'aaa')

--just display the first occurrence rows of each text1 value
SELECT
    *
    FROM (SELECT KeyID,text1,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber FROM @YourTable) dt
    WHERE RowNumber=1

--delete all duplicates
;WITH NumberedRows AS 
(SELECT
     KeyID,ROW_NUMBER() OVER (PARTITION BY text1 ORDER BY KeyID) AS RowNumber
     FROM @YourTable
)
DELETE NumberedRows
    WHERE RowNumber>1

select * from @YourTable  --<<display remaining rows

OUTPUT:

KeyID       text1
----------- ----------  <<from 1st select
1           aaa
2           bbb
4           ccc
7           ddd

(4 row(s) affected)


KeyID       text1
----------- ----------  <<from second select after delete
1           aaa
2           bbb
4           ccc
7           ddd

(4 row(s) affected)
小耗子 2024-12-09 11:04:39

对于您的用户界面,您只想显示唯一记录...

SELECT DISTINCT
   text1,
   text2
FROM table

对于您的更新...

UPDATE table
   SET text2 = @text2
WHERE
   text1 = @text1

正如我解释您的问题一样,您的源表(用于用户界面中的 SELECT)仅来自一个数据库(?)。然后你的更新应该被推送到每个数据库(?)。如果是这样,请使用上面的更新。如果没有 - 也许是这样:

对于您的 UI,您只想显示唯一记录...

SELECT DISTINCT
   db_name,
   text1,
   text2
FROM table

对于您的更新...

UPDATE table
   SET text2 = @text2
WHERE
   text1 = @text1
   AND db_name = @db_name

For your UI, where you want to display unique records only...

SELECT DISTINCT
   text1,
   text2
FROM table

For your update...

UPDATE table
   SET text2 = @text2
WHERE
   text1 = @text1

As I interpret your question, your source table (for your SELECT in the UI) is only coming from one database (?). And then your update should be pushed out to every database (?). If so, then use the update above. If not -- maybe this:

For your UI, where you want to display unique records only...

SELECT DISTINCT
   db_name,
   text1,
   text2
FROM table

For your update...

UPDATE table
   SET text2 = @text2
WHERE
   text1 = @text1
   AND db_name = @db_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文