sql server:如何用查询结果批量替换字段

发布于 2024-11-07 16:49:08 字数 732 浏览 0 评论 0原文

情况如下,数据库已创建但未标准化。表LUT_ProductInfo 包含一个名为flavor 的字段,该字段填充有实际的风味名称(即Coke Classic)。我创建了一个查找表 LUT_Flavors,其中包含所有带有 PK 的风味名称。我需要将 LUT_Product 信息中的所有风味名称替换为 LUT_Flavors 表中相应的外键。目前,每个表中的风味名称都是相同的,因此交换应该是干净的。

我尝试了这样的事情:

update LUT_ProductInfo 
set flavor =
     (select LUT_Flavor.id, LUT_Flavor.flavor 
      from LUT_ProductInfo prod 
      join LUT_Flavor on LUT_Flavor.flavor = prod.flavor).ID
where 
      LUT_ProductInfo.flavor = (select LUT_Flavor.id,LUT_Flavor.flavor 
                                from LUT_ProductInfo prod 
                                join LUT_Flavor
                                on LUT_Flavor.flavor = prod.flavor).flavor

但这违反了我认为的一些规则并且没有做任何事情。有人有任何见解吗?

Heres the situation, a database was created and not normalized. The table LUT_ProductInfo contains a field called flavor that is populated with an actual flavor name (ie Coke Classic). I created a lookup table LUT_Flavors with all of the flavor names complete with PKs. I need to replace all of the flavor names in LUT_Product info with the corresponding foreign key from the LUT_Flavors table. At the moment, the flavor names in each table are identical so the swap should be clean.

I tried something like this:

update LUT_ProductInfo 
set flavor =
     (select LUT_Flavor.id, LUT_Flavor.flavor 
      from LUT_ProductInfo prod 
      join LUT_Flavor on LUT_Flavor.flavor = prod.flavor).ID
where 
      LUT_ProductInfo.flavor = (select LUT_Flavor.id,LUT_Flavor.flavor 
                                from LUT_ProductInfo prod 
                                join LUT_Flavor
                                on LUT_Flavor.flavor = prod.flavor).flavor

But that broke some rules I guess and didn't do anything. Does anyone have any insight?

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

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

发布评论

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

评论(5

一杯敬自由 2024-11-14 16:49:08

抱歉,我正在路上,所以没有数据库可供尝试 - 但请尝试:

update LUT_ProductInfo 
set flavor = f.id
from  LUT_ProductInfo p, 
LUT_Flavour f
where f.flavor = p.flavor

Sorry, am on the road, so no database to try this on - but try:

update LUT_ProductInfo 
set flavor = f.id
from  LUT_ProductInfo p, 
LUT_Flavour f
where f.flavor = p.flavor
君勿笑 2024-11-14 16:49:08
Hi, What about this:



  UPDATE LUT_ProductInfo product
  SET flavor = 
  (
      select LUT_Flavor.id
      from LUT_Flavor prod 
      where prod .flavor  = product.flavor
  )
Hi, What about this:



  UPDATE LUT_ProductInfo product
  SET flavor = 
  (
      select LUT_Flavor.id
      from LUT_Flavor prod 
      where prod .flavor  = product.flavor
  )
时光是把杀猪刀 2024-11-14 16:49:08

这是我将如何做的一个例子。请注意,我向 LUT_ProductInfo 表添加了一个flavorid 列。这只是为了确保传输顺利进行,您可以稍后删除/重命名它,或者如果您勇敢的话,可以直接进入该列。不过,您可能想在更新每条记录之前备份表,但您不想丢失任何内容。

无论如何,这是我的测试代码:

CREATE TABLE #LUT_ProductInfo (
    ProdID int PRIMARY KEY,
    flavor varchar(10),
    flavorid int NULL)

INSERT INTO #LUT_ProductInfo VALUES (1,'Vanilla', NULL)
INSERT INTO #LUT_ProductInfo VALUES (2,'Chocolate', NULL)
INSERT INTO #LUT_ProductInfo VALUES (3,'Strawberry', NULL)

CREATE TABLE #LUT_Flavor (
    flavorid int PRIMARY KEY,
    flavor varchar(10))

INSERT INTO #LUT_Flavor VALUES (1,'Chocolate')
INSERT INTO #LUT_Flavor VALUES (2,'Vanilla')
INSERT INTO #LUT_Flavor VALUES (3,'Strawberry')

SELECT * FROM #LUT_ProductInfo

/* What you want is here */
UPDATE prodinfo
SET flavorid = fl.flavorid
FROM #LUT_ProductInfo AS prodinfo
INNER JOIN #LUT_Flavor AS fl
ON prodinfo.flavor = fl.flavor
/* End what you want */

SELECT * FROM #LUT_ProductInfo

DROP TABLE #LUT_ProductInfo
DROP TABLE #LUT_Flavor

Here's an example how I would do it. Note that I add a flavourid column to the LUT_ProductInfo table. This is just to ensure that the transfer went smoothly, you can delete/rename it later or just go straight into the column if you're brave. You might want to back up your table before updating every record though, you wouldn't want to lose anything.

Anyway, here's my test code:

CREATE TABLE #LUT_ProductInfo (
    ProdID int PRIMARY KEY,
    flavor varchar(10),
    flavorid int NULL)

INSERT INTO #LUT_ProductInfo VALUES (1,'Vanilla', NULL)
INSERT INTO #LUT_ProductInfo VALUES (2,'Chocolate', NULL)
INSERT INTO #LUT_ProductInfo VALUES (3,'Strawberry', NULL)

CREATE TABLE #LUT_Flavor (
    flavorid int PRIMARY KEY,
    flavor varchar(10))

INSERT INTO #LUT_Flavor VALUES (1,'Chocolate')
INSERT INTO #LUT_Flavor VALUES (2,'Vanilla')
INSERT INTO #LUT_Flavor VALUES (3,'Strawberry')

SELECT * FROM #LUT_ProductInfo

/* What you want is here */
UPDATE prodinfo
SET flavorid = fl.flavorid
FROM #LUT_ProductInfo AS prodinfo
INNER JOIN #LUT_Flavor AS fl
ON prodinfo.flavor = fl.flavor
/* End what you want */

SELECT * FROM #LUT_ProductInfo

DROP TABLE #LUT_ProductInfo
DROP TABLE #LUT_Flavor
桃气十足 2024-11-14 16:49:08

如果没有错误消息很难判断。

也许可以尝试: (select LUT_Flavor.id from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor.flavor = prod.flavor)

而不是

(select LUT_Flavor.id,LUT_Flavor.flavor from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor .flavor = prod.flavor).ID

如果没有帮助,请粘贴错误消息。

Hard to tell without the error message.

Maybe try: (select LUT_Flavor.id from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor.flavor = prod.flavor)

instead of

(select LUT_Flavor.id,LUT_Flavor.flavor from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor.flavor = prod.flavor).ID

If it doesn't help please paste error message.

゛时过境迁 2024-11-14 16:49:08

更好的方法是:

update LUT_ProductInfo
set flavor = fl.id
from LUT_ProductInfo pi
inner join LUT_Flavor fl on fl.flavor = pi.flavor

使用 from 子句并以这种方式加入。此外,此解决方案假定风味和 ID 之间的数据类型是兼容的,如果 id 是 int 并且风味是 nvarchar(x),您可能不会得到您想要的结果。您可能必须进行更新,然后更改数据类型。

A better way to do this is:

update LUT_ProductInfo
set flavor = fl.id
from LUT_ProductInfo pi
inner join LUT_Flavor fl on fl.flavor = pi.flavor

having a from clause and joining that way. Also this solution presumes that the data types between flavor and ID are compatible, if id is an int and the flavor is nvarchar(x) you might not get the results you want. You'll probably have to do the update, then change the data type.

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