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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:58:30+00:00 2026-05-28T05:58:30+00:00

I have 5 tables. One primary and 4 additional (they have different columns). objects

  • 0

I have 5 tables. One primary and 4 additional (they have different columns).

  1. objects
  2. obj_mobiles
  3. obj_tablets
  4. obj_computers

Here is the structure of my main table (objects).

ID | type | name | etc…

So what I want to do is to join objects with other (obj_mobiles,obj_tablets,…) tables, depending on type field.
I know that I should use dynamic SQL. But I can’t make procedure. I think it should look like something like this.

SELECT objects.type into @tbl FROM objects;
PREPARE stmnt FROM "SELECT * FROM objects AS object LEFT JOIN @tbl AS info ON object.id = info.obj_id"; 
EXECUTE stmnt;
DEALLOCATE PREPARE stmnt;

Aslo pseudo-code

SELECT * FROM objects LEFT JOIN [objects.type] ON ... 

Can anyone post procedure? Also I want to have all rows not just 1 row.
Thanks.

  • 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-28T05:58:31+00:00Added an answer on May 28, 2026 at 5:58 am

    If you want all rows (bulk output) and not one row at a time, the below should be fast and also the output for all rows will contain all columns.

    Lets consider below to be the fields of the tables.
    obj_mobiles – ID | M1 | M2
    obj_tablets – ID | T1 | T2
    obj_computers – ID | C1 | C2
    objects – ID | type | name | etc.,

    Select objects.*, typestable.*
    from (
        select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
        union all
        select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
        union all
        select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
            left join objects on typestable.oID = objects.ID and typestable.otype = objects.type;
    
    +------+--------------------+----------+------+----------+------+------+------+------+------+------+
    | ID   | name               | type     | ID   | type     | M1   | M2   | T1   | T2   | C1   | C2   |
    +------+--------------------+----------+------+----------+------+------+------+------+------+------+
    |    1 | Samsung Galaxy s2  | mobile   |    1 | mobile   |    1 | Thin | NULL | NULL | NULL | NULL |
    |    2 | Samsung Galaxy Tab | tablet   |    2 | tablet   | NULL | NULL | 0.98 |   10 | NULL | NULL |
    |    3 | Dell Inspiron      | computer |    3 | computer | NULL | NULL | NULL | NULL | 4.98 | 1000 |
    +------+--------------------+----------+------+----------+------+------+------+------+------+------+
    

    The table are create as below.

    mysql> create table objects (ID int, name varchar(50), type varchar (15));
    Query OK, 0 rows affected (0.05 sec)
    mysql> insert into objects values (1, "Samsung Galaxy s2", "mobile"), (2, "Samsung Galaxy Tab", "tablet"), (3, "Dell Inspiron", "computer");
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    
    mysql> create table obj_mobiles (ID int, M1 int, M2 varchar(10));
    Query OK, 0 rows affected (0.03 sec)
    mysql> insert into obj_mobiles values (1, 0.98, "Thin");
    Query OK, 1 row affected (0.00 sec)
    
    
    mysql> create table obj_tablets (ID int, T1 float, T2 int(10));
    Query OK, 0 rows affected (0.03 sec)
    mysql> insert into obj_tablets values (2, 0.98, 10);
    Query OK, 1 row affected (0.00 sec)
    
    
    mysql> create table obj_computers (ID int, C1 float, C2 int(10));
    Query OK, 0 rows affected (0.03 sec)
    insert into obj_computers values (3, 4.98, 1000);
    

    also to confirm the datatypes of the columns are same as the original columns, the result is saved into a table and datatypes are checked below.

    create table temp_result as
    Select objects.*, typestable.*
    from (
        select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
        union all
        select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
        union all
        select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
            left join objects on typestable.oID = objects.ID and typestable.otype = objects.type;
    
    mysql> desc temp_result;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | ID    | int(11)     | YES  |     | NULL    |       |
    | name  | varchar(50) | YES  |     | NULL    |       |
    | type  | varchar(15) | YES  |     | NULL    |       |
    | oID   | int(11)     | YES  |     | NULL    |       |
    | otype | varchar(8)  | NO   |     |         |       |
    | M1    | int(11)     | YES  |     | NULL    |       |
    | M2    | varchar(10) | YES  |     | NULL    |       |
    | T1    | float       | YES  |     | NULL    |       |
    | T2    | int(11)     | YES  |     | NULL    |       |
    | C1    | float       | YES  |     | NULL    |       |
    | C2    | int(11)     | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    11 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table and one of the fields (not the primary key)
I have two tables one has a three column composite key. The other needs
I have 2 tables - one storing user information (id, username, password) and the
I have some tables. These tables all have one column in common called 'classified_id':
I have used a join query for retrieving value from two tables one is
I have an edmx model which I have draged 2 tables onto - One
I have 2 tables that their rows have one on one relation.. For you
I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two tables, one stores the products and quantity we have bought, the

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.