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 263671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:37:17+00:00 2026-05-11T22:37:17+00:00

I’m pretty fresh to database handling, but now completely without experience. However, I have

  • 0

I’m pretty fresh to database handling, but now completely without experience. However, I have gotten stuck on a problem. I need to formulate an SQL query that returns all articles that are compatible to a set of other articles (of arbitrary size).
The query is to be generated by a script in an application to search for articles where the user can input a list of articles that any found articles should usable(compatible) with.

So for a list of article numbers A, B, … , N, the question is:
“Give me all articles that are compatible with A and B and … and N”

The question concerns only one table;
Compatible
artOne
artTwo

Each record in Compatible represents a compatibility relation so that articles A and B are compatible iff there is a record with article number A in one column and B in the other. NB the order doesn’t make any difference for the compatibility.

Now, given a list of articles, I want to be able to generate a query that returns all articles that are compatible.

For example, consider the table
Compatible

A  B
----
1  2
3  1
3  4

If I wanted all articles that are compatible with [1], the query would return [2, 3].
The query generated by the list [2, 3] will return [1].
Whilst the query generated from the list [1, 3] generates an empty list.

Admittedly, this is probably not the best way to tackle the problem so I welcome any better solutions as well. I reckon this type of questions require some type of subqueries, a subject which I am yet to master.

So, my question is — Is there any way to model the database so this particular problem is solved in an easier manner, or in any case, could someone help me formulating the query and how it changes with the variable amount of input. Any pointers to reading on the subject is also very much welcome.

Many thanks

Marco

  • 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-11T22:37:18+00:00Added an answer on May 11, 2026 at 10:37 pm
    SELECT  id
    FROM    (
            SELECT  B AS id
            FROM    compat
            WHERE   A IN (list)
            UNION
            SELECT  A
            FROM    compat
            WHERE   B IN (list)
            ) q
    GROUP BY
            id
    HAVING  COUNT(*) = @cnt
    

    , where @cnt is the total number of items in your list.

    For this to work, you should make sure that no compatibility pair has two entries in the table (i. e. having (1, 2) and (2, 1) at once is bad).

    It’s best to have two constraints: one ensuring that the pair is unique, the other one checking that the article with the least id to be first:

    ALTER TABLE compat ADD CONSTRAINT ux_compat_ab UNIQUE (A, B)
    ALTER TABLE compat ADD CONSTRAINT cc_compat_order CHECK (A < B)
    

    If you do this, you can replace UNION with more efficient UNION ALL:

    SELECT  id
    FROM    (
            SELECT  B AS id
            FROM    compat
            WHERE   A IN (list)
            UNION ALL
            SELECT  A
            FROM    compat
            WHERE   B IN (list)
            ) q
    GROUP BY
            id
    HAVING  COUNT(*) = @cnt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.