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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:08+00:00 2026-06-14T18:48:08+00:00

Is there any way how can I use result for specifying table to join?

  • 0

Is there any way how can I use result for specifying table to join?

I’d like to do something like

SELECT id, some_number, ... FROM sometable NATURAL JOIN someothertable_$some_number;

I know that there’s nothing like this in relational algebra, so probably I’ll not succeed, I just wanted to ask to be sure.

I don’t want to use any SQL scripts.

  • 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-14T18:48:10+00:00Added an answer on June 14, 2026 at 6:48 pm

    Runnable Example Here: http://sqlfiddle.com/#!2/5e92c/36

    Code to setup tables for this example:

    create table if not exists someTable 
    (
      someTableId bigint not null auto_increment
      , tableId int not null
      , someOtherTableId bigint not null
    
      , primary key (someTableId)
      , index (tableId, someOtherTableId)
    );
    
    create table if not exists someOtherTable_$1
    (
      someOtherTableId bigint not null auto_increment
      , data varchar(128) character set utf8
    
      , primary key (someOtherTableId)
    );
    
    create table if not exists someOtherTable_$2
    (
      someOtherTableId bigint not null auto_increment
      , data varchar(128) character set utf8
    
      , primary key (someOtherTableId)
    );
    
    insert sometable (tableId, someOtherTableId) values (1, 1);
    insert sometable (tableId, someOtherTableId) values (1, 2);
    insert sometable (tableId, someOtherTableId) values (2, 2);
    insert sometable (tableId, someOtherTableId) values (2, 3);
    
    insert someothertable_$1(data) values ('table 1 row 1');
    insert someothertable_$1(data) values ('table 1 row 2');
    insert someothertable_$1(data) values ('table 1 row 3');
    
    insert someothertable_$2(data) values ('table 1 row 1');
    insert someothertable_$2(data) values ('table 1 row 2');
    insert someothertable_$2(data) values ('table 1 row 3');
    

    STATIC SOLUTION

    Here’s a solution if your tables are fixed (e.g. in the example you only have someOtherTable 1 and 2 / you don’t need the code to change automatically as new tables are added):

    select st.someTableId
    , coalesce(sot1.data, sot2.data)
    from someTable st
    left outer join someOtherTable_$1 sot1
    on st.tableId = 1
    and st.someOtherTableId = sot1.someOtherTableId
    left outer join someOtherTable_$2 sot2
    on st.tableId = 2
    and st.someOtherTableId = sot2.someOtherTableId;
    

    DYNAMIC SOLUTION

    If the number of tables may change at runtime you’d need to write dynamic SQL. Beware: with every successive table you’re going to take a performance hit. I wouldn’t recommend this for a production system; but it’s a fun challenge. If you can describe your tool set & what you’re hoping to achieve we may be able to give you a few pointers on a more suitable way forward.

    select group_concat(distinct ' sot' , cast(tableId as char) , '.data ') 
    into @coalesceCols 
    from someTable;
    
    select group_concat(distinct ' left outer join someOtherTable_$', cast(tableId as char), ' sot', cast(tableId as char), ' on st.tableId = ', cast(tableId as char), ' and st.someOtherTableId = sot', cast(tableId as char), '.someOtherTableId ' separator '') 
    into @tableJoins 
    from someTable;
    
    set @sql = concat('select someTableId, coalesce(', @coalesceCols ,') from someTable st', @tableJoins);
    
    prepare stmt from @sql;
    execute stmt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We can't use java.text.SimpleDateFormat, so is there any way to create date object from
In my code below, is there any way I can use the results in
Is there any way I can use JPA or hibernate annotations to specify the
Is there any way I can use RegExp or Wildcard searches to quickly delete
Is there any way I can use a KMZ file in Google Maps? My
Is there any way that I can use C# to load and then render
Is there any way that I can use JavaScript (and jQuery) to create a
Is there any way I can streamline my deployment process? I currently use these
Is there any free gate way or a way that I can use to
Is there any way that I can detect browser type on routing and use

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.