?
Solved

How to easily create a select statement inner join 20 tables/

Posted on 2008-11-12
6
Medium Priority
?
232 Views
Last Modified: 2012-05-05
Hi, Is there a easy way to create a select statement that involves about 20 tables in a database?  In table0 I want to select everything.  Using the Id column in table0 I want to join that with rest of the table1-table20 which all have a id column as well.  I want all columns of table0 even if there is no match in any of the other table.  From Table1 - table20, I only want the column Name if the Id column is a match for the table 0.  Can someone show how this select query would look like?  Also, there is a lot of columns in table0, is there any way to specify to return all columns form table0 during a join?  I used to use Query AnalyZer but it's no longer avaialble in 2005.  Very annoying.
0
Comment
Question by:lapucca
  • 2
  • 2
  • 2
6 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 22945126
yes, it would be like below.

> I used to use Query AnalyZer but it's no longer avaialble in 2005.  
in sql 2005 management studio, you have the query window.
otherwise, you have the command line tool sqlcmd.

alternatively, there are some nice (and free) tools out there.

select t0.*
 , t1.name name_1
 , t2.name name_2
  ... etc ...
from table0 t0
join table1 t1
  on t1.id = t0.id
join table1 t2
  on t2.id = t0.id
 ... etc ...

Open in new window

0
 
LVL 41

Accepted Solution

by:
Sharath earned 1600 total points
ID: 22945304
>> I only want the column Name if the Id column is a match for the table 0.
if it matches it will display the column name otherwise NULL will be displayed. Is that what you want?

You need to use left join.

SELECT t0.*,
       t1.name name_1,
       t2.name name_2
       ....etc...
  FROM table0 t0
  LEFT JOIN table1 t1 ON t0.id = t1.id
  LEFT JOIN table1 t2 ON t0.id = t2.id
  .... etc....
0
 

Author Comment

by:lapucca
ID: 22945329
So, just using join as inner join for all tables?  Would this return all fields in table0 (and other matching id tables' name field) when there is no match of id in table3 or table8?  I thougth inner join only return that row if there is a match on both tables or all tables join.  Is this not the case?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22945342
if there is no match, use indeed LEFT JOIN instead of simply JOIN.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 22945426
yes, you have to use LEFT JOIN. check my post.
0
 

Author Closing Comment

by:lapucca
ID: 31516191
thank you.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

569 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