Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edited based on comments. Say you're searching for articles that:</p> <ul> <li>Have a keyword named 'a1'</li> <li>Or, have both keywoards named 'b1' and 'b2'</li> <li>Or, have either keyword 'c1', 'c2' or 'c3'</li> </ul> <p>You can query like:</p> <pre><code>select a.id from Articles a inner join ArticleKeywords ak on ak.articleid = a.id inner join Keywords k on k.id = ak.keywordid group by a.id having sum(case when k.name in ('a1') then 1 else 0 end) = 1 or sum(case when k.name in ('b1','b2') then 1 else 0 end) = 2 or sum(case when k.name in ('c1','c2,'c3') then 1 else 0 end) &gt; 0 </code></pre> <p>Per SquareCog's comment, you can greatly increase performance with an early WHERE clause. The clause would limit the grouping to the relevant keywords only. In the above query, add the WHERE just before the HAVING:</p> <pre><code>... inner join Keywords k on k.id = ak.keywordid where k.name in ('a1','b1','b2','c1','c2','c3') group by a.id ... </code></pre> <p>You can retrieve the other details of the article(s) like:</p> <pre><code>select * from Articles where id in ( ...query from above here... ) </code></pre> <p>Say you have a table that contains groups to search for, defined like:</p> <pre><code>groupid - keywordid 1 - 1 1 - 2 2 - 3 </code></pre> <p>Meaning the article has to match ((keyword 1 and keyword2) or keyword3). Then you can query like this:</p> <pre><code>select ak.articleid from ArticleKeywords ak inner join Search s on ak.keywordid = s.keywordid group by s.searchgroup, ak.articleid having count(*) = ( select count(*) from #Search s2 where s2.Searchgroup = s.SearchGroup ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload