Solved

Join unrelated tables

Posted on 2004-09-10
7
2,998 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
7 Comments
 
LVL 18

Accepted Solution

by:
ShogunWade earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
How totally cool was that answer - works a treat...

Thanks a bunch.

James.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Expert Comment

by:FDzjuba
Comment Utility
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
Comment Utility
glad to help :)

0
 

Author Comment

by:JAMES
Comment Utility
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
Comment Utility
PS.   FD,   more modern syntax for SELECT table1.*,table2.* FROM Table1,Table2   is SELECT * FROM Table1 CROSS JOIN Table2
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now