This has got me stumped I need to:
UPDATE items
SET items.Categories = category.Categories --fyi this is pk
WHERE items.Description is like pattern from category.Description
I've tried all kinds of ways to do this. Maybe I'm looking at this all wrong; however, I'm spinning my wheels on this one.
I found this to work:
UPDATE ITEMS
SET categories = C.Categories
FROM ITEMS I, CATEGORY C
WHERE I.description LIKE '%' + substring(C.description,1,6) + '%';
yields (933 row(s) affected) and every value in c.Categories shows up at least one or more times.
Is there a way make this even more effective?
UPDATE
will be like this
UPDATE ITEMS
set categories = C.Categories
FROM ITEMS I
JOIN CATEGORY C
ON I.description LIKE '%' + C.description + '%' ;
Assuming you have data set like this, XYZ will be updated as APPLE in ITEM table
INSERT INTO ITEMS
VALUES ('XYZ','APPLE is good and healthy');
INSERT INTO CATEGORY
VALUES ('APPLE','APPLE is good');
While running the update you will also have to be sure that UPDATE statement does not produce duplicates due to one to many relationship between "description" fields of the table. For this you can either run a check for one to many relationship existence or drop the primary key altogether.
Please find the code below which checks the one to many relationship and then runs the update:
IF NOT EXISTS (SELECT A.Categories, COUNT(1)
FROM items A
INNER JOIN Category B
ON A.Description LIKE '%' + B.Description + '%'
GROUP BY A.Categories
HAVING COUNT(1) > 1
)
UPDATE ITEMS
set categories = C.Categories
FROM ITEMS I
INNER JOIN CATEGORY C
ON I.description LIKE '%' + C.description + '%' ;
Also find the solution on SQL Fiddle here: http://sqlfiddle.com/#!6/80e2e/12
Is this what you want?
UPDATE items
SET Categories = c.Categories --fyi this is pk
FROM items i JOIN
Category c
ON i.Description like '%' + category.Description + '%';
As a note: the comparison between the tables is using a wildcard match, which is going to be slow. You cannot improve this particular comparison with a normal index, although a full text index might help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.