将一个表中的字符串排除在另一个表之外



我有一个名为tblExclusions的表。它有一个名为ProductCode的列,其中包含我要排除在另一个表tblSubscription中的单词,该表的列为Product_Code。Product_Code包含的字符串可能包含要使用tblExclusions数据排除的单词。

tbl排除样品:

产品代码(标题(

所有型号

WireFrame

扩展

tbl订阅示例:

产品代码(标题(

C233与所有型号

线框网格H456

G789 扩展

D132,F345,G567

DB07

输出应为:产品代码

D132,F345,G567

DB07

我尝试了一些我认为有效的东西:

SELECT b.product_code
FROM
ttblExclusions a
LEFT JOIN tblSubscription b
ON  b.product_code  Not LIKE ("*" &  a.ProductCode & "*")

它返回了记录,但仍然包含带有我想要排除的字符串的行。有没有办法调整它来满足我的需要?

Scott

根据上面的解释,我们似乎想从tblSubscription表中选择product_code,而这是不可用的表(ttblExclusions(。下面的查询将达到目的:

SELECT 
a.product_code 
FROM 
tblSubscription a 
where 
a.product_code not in (
select 
distinct ProductCode 
from 
ttblExclusions
);

我最终使用了一个记录集来循环遍历tblExclusion数据。然后使用Instr查看是否匹配。如果是这样,我运行了一个更新查询来标记Delete列。

Do While Not rs.EOF
strProdCode = rs("ProductCode")
DoCmd.RunSQL ("UPDATE tblSubscription SET tblSubscription.[Delete] = 'Yes' WHERE (((InStr([Product_Code],'" & strProdCode & "'))>0));")
rs.MoveNext
Loop
Then I just deleted the flagged records.

可能最简单的解决方案如下:

WITH tblExclusions AS (
SELECT 'AllModels' AS product_code
UNION
SELECT 'WireFrame'
UNION
SELECT 'Extension'
),
tblSubscription AS (
SELECT 'C233 with AllModels' AS product_code
UNION
SELECT 'WireFrame Grid H456'
UNION
SELECT 'Extension with G789'
UNION
SELECT 'D132, F345, G567'
UNION
SELECT 'DB07'
)
SELECT s.product_code 
FROM tblExclusions e 
RIGHT JOIN tblSubscription s ON s.product_code LIKE '%' + e.product_code + '%'
WHERE e.product_code IS NULL

最新更新