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

  • Home
  • SEARCH
  • 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 1053595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:16:38+00:00 2026-05-16T17:16:38+00:00

My database schema looks like this: Table t1: id valA valB Table t2: id

  • 0

My database schema looks like this:

Table t1:
    id
    valA
    valB

Table t2:
    id
    valA
    valB

What I want to do, is, for a given set of rows in one of these tables, find rows in both tables that have the same valA or valB (comparing valA with valA and valB with valB, not valA with valB). Then, I want to look for rows with the same valA or valB as the rows in the result of the previous query, and so on.

Example data:

t1 (id, valA, valB):
    1, a, B
    2, b, J
    3, d, E
    4, d, B
    5, c, G
    6, h, J

t2 (id, valA, valB):
    1, b, E
    2, d, H
    3, g, B


Example 1:

Input: Row 1 in t1
Output: 
    t1/4, t2/3
    t1/3, t2/2
    t2/1
    ...


Example 2:

Input: Row 6 in t1
Output:
    t1/2
    t2/1

I would like to have the level of the search at that the row was found in the result (e.g. in Example 1: Level 1 for t1/2 and t2/1, level 2 for t1/5, …) A limited depth of recursion is okay. Over time, I maybe want to include more tables following the same schema into the query. It would be nice if it was easy to extend the query for that purpose.

But what matters most, is the performance. Can you tell me the fastest possible way to accomplish this?

Thanks in advance!

  • 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-16T17:16:39+00:00Added an answer on May 16, 2026 at 5:16 pm

    try this although it’s not fully tested but looked like it was working 😛 (http://pastie.org/1140339)

    drop table if exists t1;
    create table t1
    (
    id int unsigned not null auto_increment primary key,
    valA char(1) not null,
    valB char(1) not null
    )
    engine=innodb;
    
    drop table if exists t2;
    create table t2
    (
    id int unsigned not null auto_increment primary key,
    valA char(1) not null,
    valB char(1) not null
    )
    engine=innodb;
    
    drop view if exists t12;
    create view t12 as
    select 1 as tid, id, valA, valB from t1
    union
    select 2 as tid, id, valA, valB from t2;
    
    insert into t1 (valA, valB) values 
    ('a','B'),
    ('b','J'),
    ('d','E'),
    ('d','B'),
    ('c','G'),
    ('h','J');
    
    insert into t2 (valA, valB) values 
    ('b','E'),
    ('d','H'),
    ('g','B');
    
    drop procedure if exists find_children;
    
    delimiter #
    
    create procedure find_children
    (
    in p_tid tinyint unsigned,
    in p_id int unsigned 
    )
    proc_main:begin
    
    declare done tinyint unsigned default 0;
    declare dpth smallint unsigned default 0;
    
    
    create temporary table children(
     tid tinyint unsigned not null,
     id int unsigned not null,
     valA char(1) not null,
     valB char(1) not null,
     depth smallint unsigned default 0,
     primary key (tid, id, valA, valB)
    )engine = memory;
    
    insert into children select p_tid, t.id, t.valA, t.valB, dpth from t12 t where t.tid = p_tid and t.id = p_id; 
    
    create temporary table tmp engine=memory select * from children;
    
    /* http://dec.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */
    
    while done <> 1 do
    
        if exists(
        select 1 from t12 t 
          inner join tmp on tmp.valA = t.valA or tmp.valB = t.valB and tmp.depth = dpth) then
    
            insert ignore into children
            select 
            t.tid, t.id, t.valA, t.valB, dpth+1 
          from t12 t
          inner join tmp on tmp.valA = t.valA or tmp.valB = t.valB and tmp.depth = dpth;
    
            set dpth = dpth + 1;            
    
            truncate table tmp;
            insert into tmp select * from children where depth = dpth;
    
        else
            set done = 1;
        end if;
    
    end while;
    
    select * from children order by depth;
    
    drop temporary table if exists children;
    drop temporary table if exists tmp;
    
    end proc_main #
    
    
    delimiter ;
    
    
    call find_children(1,1);
    
    call find_children(1,6);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a legacy database schema that looks like this: product_table table
The basic table schema looks something like this (I'm using MySQL BTW): integer unsigned
I execute SQL scripts to change the database schema. It looks something like this:
I wrote the database schema (only one table so far), and the INSERT statements
An application that I'm facing at a customer, looks like this: it allows end
Currently I'm using Fluent NHibernate to generate my database schema, but I want the
My google search skills have failed me, and I am not a database expert
I apologise for the long-winded, rantyness of this but I'm up at 3am dreading
I'm working on a catalog site where users can browse categories. Categories can contain
Our company is developing an internal project to parse text files. Those text files

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.