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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:35:35+00:00 2026-06-04T06:35:35+00:00

I have a 2 tables with a many-2-many relationship: tableA (ida, valA) tableB (idb,

  • 0

I have a 2 tables with a many-2-many relationship:
tableA (ida, valA)
tableB (idb, valB)
tableAB (ida, idb)

I need one query that should return in every tupple:
(ida, idb, 0/1 if related or not)

Here is some input / output example:
tableA

ida   valA
1     b
2     s
3     l

tableB

idb   valB
11    aaa
22    bbb
33    ccc

tableAB

ida   idb
1     11
2     33

Expected results:

ida      idb      is_exists
1        11       1
2        11       0
1        33       0
2        33       1

Note that I don’t need permutations which are always 0, for example ida=3 or idb=22
this is because it means that in a matrix the whole row or column is 0 (this shows that the row has no relation whatsoever with the other table)

   \ ida |     |     |  
idb \    |  1  |  2  |  3
---------------------------
11       |  1  |  0  |  0
---------------------------
22       |  0  |  0  |  0
---------------------------
33       |  0  |  1  |  0
  • 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-06-04T06:35:36+00:00Added an answer on June 4, 2026 at 6:35 am

    What you need is a driving table that has all possible pairs. One way to get this is from TableA and TableB, using a cross join:

    select allAB.aid, allAB.bid, max(case when ab.aid is not null then 1 else 0 end) as HasPair
    from (select distinct a.id as aid, b.id as bid
          from TableA a cross join
               TableB b
         ) as allAB left outer join
         TableAB ab
         on allAB.aid = ab.aid and
            allAB.bid = ab.bid
    group by allAB.aid, allAB.bid
    

    After that, the query just summarizes and determines whether there is a matching record in TableAB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 NHibernate entities (tables) that are in a one to many relationship.
I have TableA in a many-to-many relationship with TableC via TableB. That is, TableA
Is there a way to map one-to-many relationship between two tables that have the
I have two tables that have a one-to-many relationship table1 (one), table2 (many). table1
i have a many to many table relationship that involves 2 logical tables. Record
I have a couple of tables with a many-to-one relationship, and I'm trying to
I hate these three tables that. Two tables have a many to many relationship
I have a one to many relationship between tables A (columns ID and DESC
I have two tables/entities client and site that have a many to many relationship
I have two tables: Item and ItemHistory with a one to many relationship. I

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.