修复孤立记录的 UPDATE 查询

发布于 2024-09-01 10:10:06 字数 989 浏览 4 评论 0原文

我有一个 Access 数据库,其中有两个通过 PK/FK 关联的表。不幸的是,数据库表允许重复/冗余记录,并使数据库有点混乱。我正在尝试找出一条可以解决该问题的 SQL 语句。

为了更好地解释问题和目标,我创建了示例表作为参考: 替代文本 http://img38.imageshack.us/img38/9243/514201074110am.png< /a> 您会注意到有两个表,一个 Student 表和一个 TestScore 表,其中 StudentID 是 PK/FK。

Student 表包含学生 John、Sally、Tommy 和 Suzy 的重复记录。换句话说,StudentID 为 1 和 5 的 John 是同一个人,Sally 2 和 6 是同一个人,依此类推。

TestScore 表将测试分数与学生相关联。

忽略 Student 表如何/为什么允许重复等 - 我试图实现的目标是更新 TestScore 表,以便它用相应的启用的 StudentID 替换已禁用的 StudentID。因此,所有 StudentID = 1 (John) 将更新为 5;所有 StudentID = 2 (Sally) 将更新为 6,依此类推。这是我正在拍摄的结果 TestScore 表(请注意,不再有任何对禁用的 StudentID 1-4 的引用): 替代文本 http://img163.imageshack.us/img163/1954/514201091121am.png< /a> 您能想到一个可以实现此目标的查询(与 MS Access 的 JET 引擎兼容)吗?或者,也许您可​​以提供一些提示/观点,为我指明正确的方向。

谢谢。

I have an Access database that has two tables that are related by PK/FK. Unfortunately, the database tables have allowed for duplicate/redundant records and has made the database a bit screwy. I am trying to figure out a SQL statement that will fix the problem.

To better explain the problem and goal, I have created example tables to use as reference:
alt text http://img38.imageshack.us/img38/9243/514201074110am.png
You'll notice there are two tables, a Student table and a TestScore table where StudentID is the PK/FK.

The Student table contains duplicate records for students John, Sally, Tommy, and Suzy. In other words the John's with StudentID's 1 and 5 are the same person, Sally 2 and 6 are the same person, and so on.

The TestScore table relates test scores with a student.

Ignoring how/why the Student table allowed duplicates, etc - The goal I'm trying to accomplish is to update the TestScore table so that it replaces the StudentID's that have been disabled with the corresponding enabled StudentID. So, all StudentID's = 1 (John) will be updated to 5; all StudentID's = 2 (Sally) will be updated to 6, and so on. Here's the resultant TestScore table that I'm shooting for (Notice there is no longer any reference to the disabled StudentID's 1-4):
alt text http://img163.imageshack.us/img163/1954/514201091121am.png
Can you think of a query (compatible with MS Access's JET Engine) that can accomplish this goal? Or, maybe, you can offer some tips/perspectives that will point me in the right direction.

Thanks.

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

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

发布评论

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

评论(2

热血少△年 2024-09-08 10:10:06

做到这一点的唯一方法是通过一系列查询和临时表。

首先,我将创建以下 Make Table 查询,您将使用它来创建错误 StudentID 到正确 StudentID 的映射。

Select S1.StudentId As NewStudentId, S2.StudentId As OldStudentId 
Into zzStudentMap
From Student As S1
    Inner Join Student As S2
        On S2.Name = S1.Name
Where S1.Disabled = False
    And S2.StudentId <> S1.StudentId
    And S2.Disabled = True

接下来,您将使用该临时表用正确的 StudentID 更新 TestScore 表。

Update TestScore
    Inner Join zzStudentMap
        On zzStudentMap.OldStudentId = TestScore.StudentId
Set StudentId = zzStudentMap.NewStudentId

The only way to do this is through a series of queries and temporary tables.

First, I would create the following Make Table query that you would use to create a mapping of the bad StudentID to correct StudentID.

Select S1.StudentId As NewStudentId, S2.StudentId As OldStudentId 
Into zzStudentMap
From Student As S1
    Inner Join Student As S2
        On S2.Name = S1.Name
Where S1.Disabled = False
    And S2.StudentId <> S1.StudentId
    And S2.Disabled = True

Next, you would use that temporary table to update the TestScore table with the correct StudentID.

Update TestScore
    Inner Join zzStudentMap
        On zzStudentMap.OldStudentId = TestScore.StudentId
Set StudentId = zzStudentMap.NewStudentId
假面具 2024-09-08 10:10:06

识别表中重复项的最常见技术是按表示重复记录的字段进行分组:

ID  FIRST_NAME  LAST_NAME
1   Brian   Smith
3   George  Smith
25  Brian   Smith

在本例中,我们要删除其中一条 Brian Smith 记录,或者在您的情况下,更新 ID 字段,以便它们都具有值25 或 1(完全任意使用哪一个)。

SELECT  min(id)
    FROM example
GROUP BY first_name, last_name

在 ID 上使用 min 将返回:

ID  FIRST_NAME  LAST_NAME
1   Brian   Smith
3   George  Smith

如果你使用 max 你会得到

ID  FIRST_NAME  LAST_NAME
25  Brian   Smith
3   George  Smith

我通常使用这种技术来删除重复项,而不是更新它们:

DELETE FROM example
      WHERE ID NOT IN (SELECT   MAX (ID)
                           FROM example
                       GROUP BY first_name, last_name)

The most common technique to identify duplicates in a table is to group by the fields that represent duplicate records:

ID  FIRST_NAME  LAST_NAME
1   Brian   Smith
3   George  Smith
25  Brian   Smith

In this case we want to remove one of the Brian Smith Records, or in your case, update the ID field so they both have the value of 25 or 1 (completely arbitrary which one to use).

SELECT  min(id)
    FROM example
GROUP BY first_name, last_name

Using min on ID will return:

ID  FIRST_NAME  LAST_NAME
1   Brian   Smith
3   George  Smith

If you use max you would get

ID  FIRST_NAME  LAST_NAME
25  Brian   Smith
3   George  Smith

I usually use this technique to delete the duplicates, not update them:

DELETE FROM example
      WHERE ID NOT IN (SELECT   MAX (ID)
                           FROM example
                       GROUP BY first_name, last_name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文