2016-09-20 3 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. . . 아마 배열을 인쇄하는 방법 일 것입니다. 아마도'array_agg()'대신에'string_agg()'를 사용해야 할 것입니다. –

관련 문제