需要将一个字段与另一个值上有逗号的字段相匹配

Need to match a field with another that has commas on it' s value

我想将 TABLE 1 中的“FORMULATION”字段值匹配到 table 2 中的“C_TEST_ARTICLE”,这些公式中有多个用逗号分隔。

Table 1:

+----------------+--------------------+
| SAMPLE_NUMBER  |    FORMULATION     | 
+----------------+-----------+--------+
|     84778      | S/200582/01-TA-002 |      
|     84777      | S/200582/01-TA-002 |
|     81691      | S/200451/01-TA-011 |
|     81690      | S/200451/01-TA-011 |
+----------------+-----------+--------+

TABLE 2

+-----------------------+--------------------------------------+------------------+
|                     C_TEST_ARTICLE                           | C_REPORT_NUMBER  | 
+----------------+-----------+---------------------------------+------------------+
|      S/200180/03-TA-001,S/200180/03-TA-002                   |       16698      |      
| S/200375/01-TA-001,S/200375/01-TA-002,S/200375/01-TA-003     |       15031      |
+--------------------------------------------------------------+------------------+

我想要的是所有这些“C_TEST_ARTICLES”中的每一个都有一个“C_REPORT_NUMBER”,所以我想得到所有这些“[=23=” ]" 来自 table 1,这样一来,我就会得到与报告编号相关的样本。

你可以尝试使用 LIKE

select SAMPLE_NUMBER  
from table1 
INNER JOIN table2 ON c_test_article like concat('%', formulation , '%'')
select
 C_TEST_ARTICLE
,C_REPORT_NUMBER
,b1.SAMPLE_NUMBER
from TABLE 2
INNER JOIN TABLE 1 as b1 on C_TEST_ARTICLE like '%'+FORMULATION+'%'

尝试

SELECT
        T1.SampleNumber
    ,   T2.C_Report_Number
FROM    Table1  T1
    ,   Table2  T2
WHERE   CHARINDEX(T1.Formulation, T2.C_Test_article) > 0