2016-09-20 111 views
0

我想查找两个表之间的匹配词。 此查询运作良好,但我想获得完全匹配的单词以及id。从搜索查询返回匹配词

SELECT id 
FROM debug_fullText 
where title ~~* any (select matchWords from debug_matchWords) 

这里是debug_fullText:

id  |title  |tags         |description 
3893382135|"Tate Modern"|"london;londra;nut;squirrel;Westminster"|"Later that day I got to thinking about relationships. 

和速配是:

id|words 
1 |"Westminister" 
2 |"Tate Modern" 
3 |"South Afrika" 
4 |"London" 

回答

1

可能有其他的方法,但这里是一个:

SELECT ft.id, mw.words 
FROM debug_fullText ft, lateral 
    (select array_agg(matchword) as words, count(*) as cnt 
     from debug_matchwords 
     where title ilike matchWord 
    ) mw 
where mw.cnt > 0; 

这里是一个例子:

with debug_fulltext as (
     select 1 as id, 'a b c'::text as title 
    ), 
    debug_matchwords(matchword) as (
     values ('%a%'::text), ('%b%') 
    ) 
select ft.id, mw.words 
from debug_fullText ft, lateral 
    (select array_agg(matchword) as words, count(*) as cnt 
     from debug_matchwords 
     where title ilike matchWord 
    ) mw 
where mw.cnt > 0; 

这将返回两个单词。

编辑II:

这将返回比赛对我来说:

with debug_fulltext as (
     select 3893382135 as id, 'Tate Modern'::text as title 
    ), 
    debug_matchwords(id, matchword) as (
     values (1, 'Westminister'), 
      (2 , 'Tate Modern'), 
      (3 , 'South Afrika'), 
      (4 , 'London') 
    ) 
SELECT ft.id, mw.words 
FROM debug_fullText ft, lateral 
    (select array_agg(matchword) as words, count(*) as cnt 
     from debug_matchwords 
     where title ilike matchWord 
    ) mw 
where mw.cnt > 0; 

如果它不为你工作,那么有可能是字符集问题还是人品不好伪装,比如说,如空间。

+0

感谢它的工作。 :) 当你说其他方法, 你的意思是可能有更快或不必要的其他方法! – Raha1986

+0

但它不返回所有匹配的单词!它发现第一场比赛时停止! – Raha1986

+0

@ Raha1986。 。 。这可能是您打印数组的方式。也许你应该使用'string_agg()'而不是'array_agg()'。 –

相关问题