我想基于不同行的不同通配符来连接Access中的两个表。
第一个table1
包含具有不同通配符的行,而table2
包含应与table1
中的通配符匹配的列。
我想象SQL代码看起来像这样:
SELECT *
FROM table2
LEFT JOIN table1
ON table2.subject LIKE table1.wildcard
表格如下:https://imgur.com/a/O9OPAL6
第三张图片显示了我想要的结果。
如何执行联接或有替代方法?
答案 0 :(得分:2)
我不认为MySQL支持JOIN
的非相等条件。因此,您可以按照以下方式进行操作:
SELECT * -- first get the matches
FROM table2 as t2, -- ugg, why doesn't it support CROSS JOIN
table1 as t1
WHERE t2.subject LIKE t1.wildcard
UNION ALL
SELECT * -- then get the non-matches
FROM table2 as t2 LEFT JOIN
table1 as t1
ON 1 = 0 -- always false but gets the same columns
WHERE NOT EXISTS (SELECT 1
FROM table1 as t1
WHERE t2.subject LIKE t1.wildcard
);