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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:31:26+00:00 2026-05-12T07:31:26+00:00

I want to learn how to combine two db tables which have no fields

  • 0

I want to learn how to combine two db tables which have no fields in common. I’ve checked UNION but MSDN says :

The following are basic rules for combining the result sets of two queries by using UNION:

  1. The number and the order of the columns must be the same in all queries.
  2. The data types must be compatible.

But I have no fields in common at all. All I want is to combine them in one table like a view.

So what should I do?

  • 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-12T07:31:27+00:00Added an answer on May 12, 2026 at 7:31 am

    There are a number of ways to do this, depending on what you really want. With no common columns, you need to decide whether you want to introduce a common column or get the product.

    Let’s say you have the two tables:

    parts:              custs:
    +----+----------+   +-----+------+
    | id | desc     |   |  id | name |
    +----+----------+   +-----+------+
    |  1 | Sprocket |   | 100 | Bob  |
    |  2 | Flange   |   | 101 | Paul |
    +----+----------+   +-----+------+
    

    Forget the actual columns since you’d most likely have a customer/order/part relationship in this case; I’ve just used those columns to illustrate the ways to do it.

    A cartesian product will match every row in the first table with every row in the second:

    > select * from parts, custs;
          id desc     id  name
          -- ----     --- ----
          1  Sprocket 101 Bob
          1  Sprocket 102 Paul
          2  Flange   101 Bob
          2  Flange   102 Paul
    

    That’s probably not what you want since 1000 parts and 100 customers would result in 100,000 rows with lots of duplicated information.

    Alternatively, you can use a union to just output the data, though not side-by-side (you’ll need to make sure column types are compatible between the two selects, either by making the table columns compatible or coercing them in the select):

    > select id as pid, desc, null as cid, null as name from parts
      union
      select null as pid, null as desc, id as cid, name from custs;
        pid desc     cid name
        --- ----     --- ----
                     101 Bob 
                     102 Paul
        1   Sprocket
        2   Flange
    

    In some databases, you can use a rowid/rownum column or pseudo-column to match records side-by-side, such as:

    id desc     id  name
    -- ----     --- ----
    1  Sprocket 101 Bob
    2  Flange   101 Bob
    

    The code would be something like:

    select a.id, a.desc, b.id, b.name
    from parts a, custs b
    where a.rownum = b.rownum;
    

    It’s still like a cartesian product but the where clause limits how the rows are combined to form the results (so not a cartesian product at all, really).

    I haven’t tested that SQL for this since it’s one of the limitations of my DBMS of choice, and rightly so, I don’t believe it’s ever needed in a properly thought-out schema. Since SQL doesn’t guarantee the order in which it produces data, the matching can change every time you do the query unless you have a specific relationship or order by clause.

    I think the ideal thing to do would be to add a column to both tables specifying what the relationship is. If there’s no real relationship, then you probably have no business in trying to put them side-by-side with SQL.

    If you just want them displayed side-by-side in a report or on a web page (two examples), the right tool to do that is whatever generates your report or web page, coupled with two independent SQL queries to get the two unrelated tables. For example, a two-column grid in BIRT (or Crystal or Jasper) each with a separate data table, or a HTML two column table (or CSS) each with a separate data table.

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

Sidebar

Related Questions

I'm not a graphics person, but I want to learn silverlight development. Which expression
I want to learn the answer for different DB engines but in our case;
I want to learn how to create truly robust applications in .net - ones
I want to learn how to use Struts 2 and I created a simple
I'm want to learn a desktop programming language, preferably C, C++ or C#. I'm
I currently learning about scrum and want to learn from experienced professionals in the
I wonder why would a C++, C#, Java developer want to learn a dynamic
I want to see a basic project to learn how to organize membership and
I want to go backwards and learn more about how compilers, processors and memory
Im starting to learn RoR and i want to make my personal blog in

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.