我发现这个query:
CREATE TABLE tableA (string_a text);
INSERT INTO tableA(string_a) VALUES
('the manual is great'), ('Chicken chicken chicken'), ('bork');
CREATE TABLE tableB(candidate_str text);
INSERT INTO tableB(candidate_str) VALUES
('man'),('great'),('chicken');
SELECT string_a
FROM tableA
WHERE string_a LIKE ANY (SELECT '%'||candidate_str||'%' FROM tableB);
结果:
the manual is great
chicken chicken chicken
问题:
怎样才能有这个新结果?
the manuel is great | great
chicken chicken chicken | chicken
最佳答案
使用联接:
SELECT a.string_a, b.candidate_str
FROM tableA a
JOIN tableB b ON a.string_a LIKE '%'||b.candidate_str||'%';
注意,这将显示两次带有
the manual is great
的行,因为candidate_str
与man
中的manual
和单词great
匹配。这可以通过使用distinct on
只返回tableA
一次的行来改变。