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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:18:20+00:00 2026-06-09T22:18:20+00:00

I have a table (T1) and a table with attributes (T2). I’m looking to

  • 0

I have a table (T1) and a table with attributes (T2). I’m looking to find records that have the same attributes as a record with provided id.

Here’s the example. Given 1 I want to find 2 (ensuring that attributes match as well).

T1
ID | A | B
----------
1  | k | l
2  | k | l


T2
IDFK | C | D
-------------    
1    | w | x
1    | y | z
2    | w | x
2    | y | z

Here’s the SQL I have so far:

SELECT * FROM T1 
JOIN T1 AS T1COPY ON T1.A = T1COPY.A, T1.B = T1COPY.B 
JOIN T2 ON T1.ID = T2.IDFK 
JOIN T2 AS T2COPY ON T1COPY.ID = T2COPY.IDFK 
   AND T2.C = T2COPY.C 
   AND T2.D = T2COPY.D
WHERE T1.ID = 1

but it’s not working right as it’s matching 2 even if attributes are different.

  • 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-09T22:18:22+00:00Added an answer on June 9, 2026 at 10:18 pm

    Here’s the answer for MySQL: http://www.sqlfiddle.com/#!2/ec4fa/2

    select h.* 
    from 
    (
        select x.*
        from t join t x using(a,b)
        where t.id = 1 and x.id <> 1  
    ) h
    join 
    (
    
        select coalesce(x.cpIdFk, x.uIdFk) as idFk  
        from
        (
          select cp.idFk as cpIdFk, u.idFk as uIdFk
          from 
          (
            select t.id as idFk, x.*
            from t cross join (select c, d from u where idFk = 1) as x
            where t.id <> 1      
          ) cp
          left join (select * from u where idFk <> 1) u using(idfk,c,d)
    
          union
    
          select cp.idFk,u.idFk
          from 
          (
            select t.id as idFk, x.*
            from t cross join (select c, d from u where idFk = 1) as x
            where t.id <> 1      
          ) cp
          right join (select * from u where idFk <> 1) u using(idfk,c,d)
    
        ) as x
    
        group by idFk
        having bit_and(cpidFk is not null and uIdFk is not null)
    
    ) d on d.idFk = h.id 
    order by h.id;
    

    Output for filter ID == 1:

    | ID | A | B |
    --------------
    |  2 | k | l |
    |  5 | k | l |
    

    From these inputs:

    CREATE TABLE t
        (ID int, A varchar(1), B varchar(1));
    
    INSERT INTO t
        (ID, A, B)
    VALUES
        (1, 'k', 'l'),
        (2, 'k', 'l'),
        (3, 'k', 'l'),
        (4, 'k', 'l'),
        (5, 'k', 'l'),
        (6, 'k', 'j');
    
    
    CREATE TABLE u
        (IDFK int, C varchar(1), D varchar(1));
    
    INSERT INTO u
        (IDFK, C, D)
    VALUES
        (1, 'w', 'x'),
        (1, 'y', 'z'),
    
        (2, 'w', 'x'),
        (2, 'y', 'z'),
    
        (3, 'w', 'x'),
        (3, 'y', 'z'),
        (3, 'm', 'z'),
    
        (4, 'w', 'x'),
    
        (5, 'w', 'x'),
        (5, 'y', 'z'),
    
        (6, 'w', 'x'),
        (6, 'y', 'z');
    

    Explanation here: Find duplicates across multiple tables

    MySQL query look a little bit convoluted as it doesn’t support FULL JOIN and it doesn’t have CTEs too. We simulate FULL JOIN by unioning the result of LEFT JOIN and RIGHT JOIN

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

Sidebar

Related Questions

I have a database that has a table email_eml that stores 3 attributes name_eml,
I have table with 300 000 records (MyISAM). I get record from this table
I have a table containing attributes Id, Emp_name, dept_name, salary . Now i want
For example, I have a table that stores classes, and a table that stores
I have a table of different attributes I want to sum, and then group
Lets say I want to save function in database. For example we have table
I want to read database tables with Attributes. I have Table in database and
Say I have a table that has items and attributes listed like, frog green
I have table cells with attributes data-position-x and data-position-y with integer values. I want
I have a mysql table with contents the structure is here: I want to

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.