[英]How to compare two different comma separated string values and check if ANY of the values from a different comma separated string exists in the first
I hope I can explain this well. 我希望我能解释清楚。 If something is not clear, please ask
如果不清楚,请询问
I have a query to retrieve data for a User where he/she has access to to certain entities. 我有一个查询来检索用户的数据,该用户可以访问某些实体。
Basically in my user table there is a column where the user can have one or multiple comma separate integers. 基本上,在我的用户表中有一列,用户可以在其中拥有一个或多个逗号分隔的整数。 Here are some examples of what it will look like:
以下是一些示例:
TABLE #1 表格1
User #1 value: ,1,
User #2 value: ,1,3,5,12,
User #3 value: ,12,
User #4 value: ,14,3,5,
User #5 value: ,14,
My seconds table is has a very similar field. 我的秒表有一个非常相似的字段。 If I have a records in my second table similar to this:
如果我在第二张表中有一条类似于以下的记录:
TABLE #2 表#2
User #1 value: ,3,
User #2 value: ,3,
User #3 value: ,3,
User #4 value: ,10,
User #5 value: ,11,5,12,
User #6 value: ,5,12,
So what I am trying to achieve is to take for instance User #2 value from TABLE #1 and I want to get all Users in TABLE #2 where any of their values is contained within Users #2 values. 因此,我想实现的目标是从表#1中获取用户#2的值,而我想让表#2中的所有用户都包含在用户#2的值中。 Because User #2 has ,1,3,5,12, I should get result from TABLE #2 as follows:
由于用户#2具有,1、3、5、12,因此我应该从表#2获得结果,如下所示:
User #1
User #2
User #3
User #5
User #6
I am currently using the following: 我目前正在使用以下内容:
SELECT a.Name
FROM TABLE2 a
WHERE
CharIndex(','+convert(varchar,a.Agency)+',',(Select Agency from TABLE1 where UserID = 123)) > 0
This does not seem to be producing the results I am looking for. 这似乎并没有产生我想要的结果。 This will work great if there is only 1 value from TABLE #1 for example ,3, but not multiple values.
如果表#1中只有1个值(例如,3)而不是多个值,那么这将非常有用。
You can try the following query to get expected result. 您可以尝试以下查询以获得预期结果。
SELECT DISTINCT a.Name
FROM TABLE2 a,
dbo.Split((SELECT Agency
FROM TABLE1
WHERE UserID = 2),',') AS b
WHERE a.Agency LIKE '%,'+b.Slit_Func_ColumnName+',%'
Use SPLIT
function. 使用
SPLIT
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.