从另一个 table 部分匹配的列更新列
Update column from column from another table partially matched
我有两个 table,第一个 table 叫做 IM_ITEM
并且有以下列
ITEM_NO
DESCR
BRAND
10000
Orin Swift Black
" "
10001
Gold Peak Sweet tea
" "
10002
Vara Red Blend
" "
10003
Gatorade Lemon Lime
Gatorade
第二个 table 是一个名为 IM_ATTR_COD
的查找 table 并且具有以下列
ATTR_5
Description
Orin Swift
Orin Swift
Gold Peak
Gold Peak
Vara
Vara
Gatorade
Gatorade
Comfort
Southern Comfort
我想用table“[=39=”中“ATTR_5”列的数据更新table“IM_ITEM”中的空字段“BRAND” ]”。当 ATTR_5 部分匹配来自 table IM_ITEM
的“DESCR”中的数据时
我使用了下面的代码,但是没有正常工作
UPDATE IM_ITEM
SET BRAND = ATTR_5
FROM IM_ITEM M
RIGHT JOIN IM_ATTR_COD R ON M.BRAND = R.ATTR_5
WHERE CHARINDEX (R.ATTR_5, M.DESCR) > 0
预期结果
ITEM_NO
DESCR
BRAND
10000
Orin Swift Black
Orin Swift
10001
Gold Peak Sweet tea
Gold Peak
10002
Vara Red Blend
Vara
10003
Gatorade Lemon Lime
Gatorade
任何帮助,请
没有 concat 那么:
UPDATE m
SET m.BRAND = r.ATTR_5
FROM IM_ITEM m
JOIN IM_ATTR_COD r
ON m.DESCR LIKE r.ATTR_5 + '%'
WHERE ISNULL(m.BRAND,'') = ''
尝试
UPDATE IM_ITEM
SET BRAND = ATTR_5
FROM IM_ITEM M
INNER JOIN IM_ATTR_COD R ON M.DESCR LIKE '%' + R.ATTR_5 + '%'
WHERE (M.BRAND = '' OR M.BRAND IS NULL)
如果您希望它应用于所有行,只需删除 WHERE (M.BRAND = '' OR M.BRAND IS NULL)
您可以使用 LIKE 子句并在 JOIN 中使用相同的子句,如下所示。
DECLARE @im_item table (ITEM_NO int, DESCR varchar(500), BRAND varchar(50))
DECLARE @im_attr_cod table (attr_5 varchar(100), DESCR varchar(500))
INSERT INTO @im_item
values
(10000,'Orin Swift Black', null)
,(10001,'Gold Peak Sweet tea',null);
INSERT INTO @im_attr_cod
values
('Orin Swift', 'Orin Swift')
,('Gold Peak', 'Gold Peak');
UPDATE i
SET BRAND = a.DESCR
FROM @im_item AS i
INNER JOIN @im_attr_cod as a
on i.DESCR like a.attr_5 + '%';
SELECT * FROM @im_item
ITEM_NO
DESCR
BRAND
10000
Orin Swift Black
Orin Swift
10001
Gold Peak Sweet tea
Gold Peak
我有两个 table,第一个 table 叫做 IM_ITEM
并且有以下列
ITEM_NO | DESCR | BRAND |
---|---|---|
10000 | Orin Swift Black | " " |
10001 | Gold Peak Sweet tea | " " |
10002 | Vara Red Blend | " " |
10003 | Gatorade Lemon Lime | Gatorade |
第二个 table 是一个名为 IM_ATTR_COD
的查找 table 并且具有以下列
ATTR_5 | Description |
---|---|
Orin Swift | Orin Swift |
Gold Peak | Gold Peak |
Vara | Vara |
Gatorade | Gatorade |
Comfort | Southern Comfort |
我想用table“[=39=”中“ATTR_5”列的数据更新table“IM_ITEM”中的空字段“BRAND” ]”。当 ATTR_5 部分匹配来自 table IM_ITEM
的“DESCR”中的数据时我使用了下面的代码,但是没有正常工作
UPDATE IM_ITEM
SET BRAND = ATTR_5
FROM IM_ITEM M
RIGHT JOIN IM_ATTR_COD R ON M.BRAND = R.ATTR_5
WHERE CHARINDEX (R.ATTR_5, M.DESCR) > 0
预期结果
ITEM_NO | DESCR | BRAND |
---|---|---|
10000 | Orin Swift Black | Orin Swift |
10001 | Gold Peak Sweet tea | Gold Peak |
10002 | Vara Red Blend | Vara |
10003 | Gatorade Lemon Lime | Gatorade |
任何帮助,请
没有 concat 那么:
UPDATE m
SET m.BRAND = r.ATTR_5
FROM IM_ITEM m
JOIN IM_ATTR_COD r
ON m.DESCR LIKE r.ATTR_5 + '%'
WHERE ISNULL(m.BRAND,'') = ''
尝试
UPDATE IM_ITEM
SET BRAND = ATTR_5
FROM IM_ITEM M
INNER JOIN IM_ATTR_COD R ON M.DESCR LIKE '%' + R.ATTR_5 + '%'
WHERE (M.BRAND = '' OR M.BRAND IS NULL)
如果您希望它应用于所有行,只需删除 WHERE (M.BRAND = '' OR M.BRAND IS NULL)
您可以使用 LIKE 子句并在 JOIN 中使用相同的子句,如下所示。
DECLARE @im_item table (ITEM_NO int, DESCR varchar(500), BRAND varchar(50))
DECLARE @im_attr_cod table (attr_5 varchar(100), DESCR varchar(500))
INSERT INTO @im_item
values
(10000,'Orin Swift Black', null)
,(10001,'Gold Peak Sweet tea',null);
INSERT INTO @im_attr_cod
values
('Orin Swift', 'Orin Swift')
,('Gold Peak', 'Gold Peak');
UPDATE i
SET BRAND = a.DESCR
FROM @im_item AS i
INNER JOIN @im_attr_cod as a
on i.DESCR like a.attr_5 + '%';
SELECT * FROM @im_item
ITEM_NO | DESCR | BRAND |
---|---|---|
10000 | Orin Swift Black | Orin Swift |
10001 | Gold Peak Sweet tea | Gold Peak |