Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 205577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:34:07+00:00 2026-05-11T17:34:07+00:00

I’m currently running some SQL which uses an IN expression to match against multiple

  • 0

I’m currently running some SQL which uses an IN expression to match against multiple IDs. However, I would ideally like to be able to match up certain IDs with others so they must appear together to return a result. Here’s an example:

Edit: The IDs I’m matching are a part of a many-to-many relationship. The structure is like this:

Articles
ArticleKeywords
Keywords

An article can have multiple Keywords linked to it by the ArticleKeywords table. I am currently using the IN expression to match any of the Keyword IDs against the records. However, I’d like to match certain records against small groups of keywords i.e. a keyword must appear with another keyword for a record to be matched.

Current:
… AND id IN (’25’, ‘566’, ‘156’, ‘166’, ‘7345’)

More specific:
… AND ((id = ’25’ AND id = ‘566’) OR (id = ‘156’ AND id = ‘166’) OR (id = ‘7345’))

Although the second option might work, I’m thinking it probably won’t be very performance savvy. Is there another way this can be done, or should I be going about it another way?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:34:07+00:00Added an answer on May 11, 2026 at 5:34 pm

    Edited based on comments. Say you’re searching for articles that:

    • Have a keyword named ‘a1’
    • Or, have both keywoards named ‘b1’ and ‘b2’
    • Or, have either keyword ‘c1’, ‘c2’ or ‘c3’

    You can query like:

    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) > 0
    

    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:

    ...
    inner join Keywords k on k.id = ak.keywordid
    where k.name in ('a1','b1','b2','c1','c2','c3')
    group by a.id
    ...
    

    You can retrieve the other details of the article(s) like:

    select *
    from Articles
    where id in (
        ...query from above here...
    )
    

    Say you have a table that contains groups to search for, defined like:

    groupid - keywordid
    1 - 1
    1 - 2
    2 - 3
    

    Meaning the article has to match ((keyword 1 and keyword2) or keyword3). Then you can query like this:

    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
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From the sounds of it you may be (a) running… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer According to the XML spec, for regular content and markup:… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode This article about reloading source code with mod_wsgi goes… May 11, 2026 at 11:31 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.