I am using .NET 4.0 & C# and SQL Server 2008. I have a table Products with a column called Keywords. I have another table Interests with 2 columns Accept and Reject. All 3 columns contains keywords separated by commas. I need to match:
- If Accept & Reject are empty, then there is a match
- If Accept has keywords, then there should be at least 1 keyword match between Accept & Keywords
- If Reject has keywords, then there should be no matches between Reject & Keywords
- Combination of 2 & 3
Ideally I’d like to do this in a SQL query. I’m open to stored procedures either Managed or T-SQL in that order.
OK, based on the comments, I’ve added these tables:
Keywords_Products [id (FK to Products), Keyword]
Keywords_Accepted [id (FK to User), Keyword]
Keywords_Rejected [id (FK to User), Keyword]
So, I need a set of products for a given userId, based on the 4 rules above.
If you are stuck with the table structure as-is (and I suspect that you are), then you’re probably best off breaking this up into multiple pieces. Your first piece is to create a function that can split strings based on a delimiter. How to do so has been answered well in another question.
It’d also help if you create a refinement on the split function by creating one specific to the Keywords field on the Products table. Something where you can pass in the ProductId and have it return the keywords for that ProductId.
From there, you can select interests matching a given product with something like