I have a table full of posts with slightly inaccurate descriptions due to an error in Excel before importing the data. I have since imported a corrected version into a new table but need to update the description in the old table based on matching one column with unique strings in the table with descriptions in the old one containing them.
So, if table A looks something like:
image
/uploads/keyword-keyword-4578.jpg
/uploads/keyword-4579-keyword.jpg
/uploads/keyword-keyword-keyword-4580.jpg
description
some incorrect description with 4578 in it somewhere
some incorrect description with 4579 in it somewhere
some incorrect description with 4580 in it somewhere
And Table B has something like
uniqueid
4578
4579
4580
description
new description
new description 2
new description 3
How should I write a query using SQL Server which will update the description in Table A with the description from Table B if the uniqueid is present in either the image or description from Table A?
There is no way to create a primary/foreign key relationship between the two tables because uniqueid is a field from the original excel sheet with no corresponding column in the posts table which is Table A.
I have tried a cross join but that just creates infinite loops or case where the data does not match up. Then I tried an inner join after seeing the first posted answer which is why I changed this question to make clear that the id number in the image occurs after different combinations of keyword and sometime before other keywords. I tried joining where title = title in both tables because the titles should be unique and matching, but that produces slightly more rows than there are in the second table. Finally, I tried using:
INNER JOIN TableA ON TableB.uniqueid like '%' + TableA.Image + '%'
That results in an infinite loop. Could I possible use CONTAINS or an index type function?
1条答案
按热度按时间5cnsuln71#
In this demo I have opted to add strings to the [uniqueid] instead of trying to derive a number from the strings in [image]. This enables a join between the 2 tables for the update. This works for the sample data, hopefully also in the real data. :
fiddle
Different Approach
As matching the strings isn't working then try stripping the image column down to just digits, not I still use it as a string match and avoid converting to integers, but you could do this if you need to.