Solved

Join unrelated tables

Posted on 2004-09-10
7
3,147 Views
Last Modified: 2010-10-11
Hi,

I have two tables that are technically unrelated but I would like them all returned together in one big query.

Obviously if I try and do a normal SQL statement combining the two it results in a cartesian join.

I can't use Union as the return columns differ between the tables.

I am quite happy with NULL's in the columns where that table doesnt have that data.

Is this possible?

Thanks.

James.
0
Comment
Question by:JAMES
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Accepted Solution

by:
ShogunWade earned 500 total points
ID: 12025832
you can do it with a union but you need some placeholder columns

eg:

table1 as col1 and table2 has col2

SELECT col1,NULL from table1
UNION ALL
SELECT NULL,col2 from table2
0
 
LVL 6

Expert Comment

by:mcp111
ID: 12025894
You can use outer joins.
If you have two tables tbl1 and tbl2,

Left join will return all records from tbl1 and matching from tbl2
Right join will return all records from tbl2 and matching from tbl1.

If you post detailed table structures and desired results I can help you with the exact query.
0
 

Author Comment

by:JAMES
ID: 12025899
How totally cool was that answer - works a treat...

Thanks a bunch.

James.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 7

Expert Comment

by:FDzjuba
ID: 12025901
join

SELECT table1.col1,table2.col1,table2.col2 FROM table1 INNER JOIN table2 ON table1.id=table2.id


combine everything without any rules
SELECT table1.*,table2.* FROM Table1,Table2
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12025910
glad to help :)

0
 

Author Comment

by:JAMES
ID: 12025921
Thanks guys but ShogunWade has the answer....

mcp111 and FD - cant use joins as I stated the two tables were unrelated.

FD - if you use your second example you create a cartesian join which I had already experienced.

Thanks all.
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12025934
PS.   FD,   more modern syntax for SELECT table1.*,table2.* FROM Table1,Table2   is SELECT * FROM Table1 CROSS JOIN Table2
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question