如何进行更新查询

发布于 2024-12-20 14:53:54 字数 1067 浏览 0 评论 0原文

我有一个包含五个字段的表,

|邮政编码 |数据1 |数据2 |数据3 |代码|

我的表及其下面的数据我有这样的记录音调,因此字段 [data 1]、[data2] 和 [data 3] 有许多重复的数据,

 Zip | Data 1 | Data 2 | Data 3 | Code |
 123 | __A__ |__ B__ |__ C__ |_____ |
 234 | __A__ |__ B__ |__ C__ |_____ |
 456 | __A__ |__ B__ |______ |_____ |
 678 | __A__ |__ B__ |__ C__ |_____ |
 981 | __A__ |__ B__ |______ |_____ |

我的目标是找到匹配的行值,因此我有一组唯一的来自它们的行(我通过给我的查询创建的)

| Data 1 | Data 2 | Data 3 | Code |
| __A__ |__ B__ |__ C__ |_____ |
| __A__ |__ B__ |______ |_____ |

我想要做的就是在该查询中输入一个代码值,以便它反映在现有表上并显示所有重复值中的代码,

所以说我输入查询中的数据(我无法输入由于某种原因)

| Data 1 | Data 2 | Data 3 | Code |
| __A__ |__ B__ |__ C__ |__C1__ |
| __A__ |__ B__ |______ |__C2__ |

我想要表中的以下输出

Zip | Data 1 | Data 2 | Data 3 | Code |
123 | __A__ |__ B__ |__ C__ |__C1__ |
234 | __A__ |__ B__ |__ C__ |__C1__ |
456 | __A__ |__ B__ |______ |__C2__ |
678 | __A__ |__ B__ |__ C__ |__C1__ |
981 | __A__ |__ B__ |______ |__C2__ |

我该怎么做?

I have a table with five fields,

| Zip code | Data 1 | Data 2 | Data 3 | Code |

my table and its data below i have tones of records like this so there are many duplicate data for fields [data 1], [data2] and [data 3]

 Zip | Data 1 | Data 2 | Data 3 | Code |
 123 | __A__ |__ B__ |__ C__ |_____ |
 234 | __A__ |__ B__ |__ C__ |_____ |
 456 | __A__ |__ B__ |______ |_____ |
 678 | __A__ |__ B__ |__ C__ |_____ |
 981 | __A__ |__ B__ |______ |_____ |

my goal is to find the matching row values so i have a set of unique row from them (which i created by a query this gave me )

| Data 1 | Data 2 | Data 3 | Code |
| __A__ |__ B__ |__ C__ |_____ |
| __A__ |__ B__ |______ |_____ |

What i wanted to do with this was just enter a code value in that query so it reflects on the existing table and displays the code in all duplicate values ,

so say i enter this data in the query,( which i cannot enter for some reason )

| Data 1 | Data 2 | Data 3 | Code |
| __A__ |__ B__ |__ C__ |__C1__ |
| __A__ |__ B__ |______ |__C2__ |

i want the following output in the table

Zip | Data 1 | Data 2 | Data 3 | Code |
123 | __A__ |__ B__ |__ C__ |__C1__ |
234 | __A__ |__ B__ |__ C__ |__C1__ |
456 | __A__ |__ B__ |______ |__C2__ |
678 | __A__ |__ B__ |__ C__ |__C1__ |
981 | __A__ |__ B__ |______ |__C2__ |

How can i do this ?

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

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

发布评论

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

评论(1

貪欢 2024-12-27 14:53:54

首先,选择不同的值并将它们插入到临时表或临时表中:

TempTable
Data1
Data2
Data3
Code null

接下来,将不同的行插入到表中

insert into TempTable (Data1, Data2, Data3)
select distinct data1, data2, data3 from myTable

接下来,使用每条记录所需的任何代码更新临时表。仍然不确定您是否有一个特定的值,或者您是否希望它只是一个“唯一”值。如果是唯一值,您可以将代码设置为身份并跳过此步骤

update temptable set Code = '' where Data1 = x and Data2 = y and Data3 = z

最后,使用值更新原始表

update myTable
inner join tempTable 
   on [myTable].data1 = [tempTable].data1 
      and [myTable].data2 = [tempTable].data2 
      and [myTable].data3 = [tempTable].data3
  set [myTable].Code = [tempTable].Code;

First, select your distinct values and insert them into a temporary table or scratch table:

TempTable
Data1
Data2
Data3
Code null

Next, insert distinct rows into your table

insert into TempTable (Data1, Data2, Data3)
select distinct data1, data2, data3 from myTable

Next, update your temp table with whatever code you want for each record. Still not sure if you have a specific value in mind or if you want it to be just a "unique" value. If a unique value, you could set code to be an identity and skip this step

update temptable set Code = '' where Data1 = x and Data2 = y and Data3 = z

Lastly, update your original table with the values

update myTable
inner join tempTable 
   on [myTable].data1 = [tempTable].data1 
      and [myTable].data2 = [tempTable].data2 
      and [myTable].data3 = [tempTable].data3
  set [myTable].Code = [tempTable].Code;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文