Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
samwise
Starting Member
7 Posts |
Posted - 2005-09-26 : 14:33:38
|
| Hi all,i need to know if its possible to inner join more than two tables - meaning i have a pivot table i'm running my query on. i have few other tables which i need to retrieve data from. how can i fetch data from all tables at the same time. they all relate to the pivot table but not one another.Thanks |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-09-26 : 14:38:47
|
| It's possible to INNER JOIN as many tables as you like. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-09-26 : 14:56:11
|
| If you have tables Parent, Child, Grandchild you'll be fine. If you have Parent, Child, Cousin you'll be in trouble - every cousin will be joined with every possible Child, for a given parent - which is presumably not what you want!Kriste |
 |
|
|
samwise
Starting Member
7 Posts |
Posted - 2005-09-28 : 04:55:03
|
| i have one parent and many children, each children has something in common with the parent but nothing to do with each other. they connect only via the parent. what is the correct Syntax if i want to fetch Data from the parent and few children at the same query ?. |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2005-09-28 : 05:20:07
|
like what Sam said, use joins, read more in BOL, but to give you a start...quote: Using Inner JoinsAn inner join is a join in which the values in the columns being joined are compared using a comparison operator.In the SQL-92 standard, inner joins can be specified in either the FROM or WHERE clause. This is the only type of join that SQL-92 supports in the WHERE clause. Inner joins specified in the WHERE clause are known as old-style inner joins.This Transact-SQL query is an example of an inner join:USE pubsSELECT *FROM authors AS a INNER JOIN publishers AS p ON a.city = p.cityORDER BY a.au_lname DESCThis inner join is known as an equi-join. It returns all the columns in both tables, and returns only the rows for which there is an equal value in the join column.Here is the result set:au_id au_lname au_fname phone address city ----------- -------- -------- ------------ --------------- --------238-95-7766 Carson Cheryl 415 548-7723 589 Darwin Ln. Berkeley409-56-7008 Bennet Abraham 415 658-9932 6223 Bateman St. Berkeleystate zip contract pub_id pub_name city state country----- ----- -------- ------ --------------------- -------- ----- -------CA 94705 1 1389 Algodata Infosystems Berkeley CA USA CA 94705 1 1389 Algodata Infosystems Berkeley CA USA (2 row(s) affected)In the result set, the city column appears twice. Because there is no point in repeating the same information, one of these two identical columns can be eliminated by changing the select list. The result is called a natural join. You can restate the preceding Transact-SQL query to form a natural join. For example:USE pubsSELECT p.pub_id, p.pub_name, p.state, a.*FROM publishers p INNER JOIN authors a ON p.city = a.cityORDER BY a.au_lname ASC, a.au_fname ASCHere is the result set:pub_id pub_name state au_id au_lname au_fname------ --------------- -------- ----------- -------- -------- 1389 Algodata Infosystems CA 409-56-7008 Bennet Abraham1389 Algodata Infosystems CA 238-95-7766 Carson Cherylphone address city state zip contract--------------- ------------- -------- ----- ----- ---------415 658-9932 6223 Bateman St. Berkeley CA 94705 1415 548-7723 589 Darwin Ln. Berkeley CA 94705 1(2 row(s) affected)In this example, publishers.city does not appear in the results.Joins Using Operators Other Than EqualYou can also join values in two columns that are not equal. The same operators and predicates used for inner joins can be used for not-equal joins. For more information about the available operators and predicates that can be used in joins, see Using Operators in Expressions and WHERE. This Transact-SQL example is of a greater-than (>) join which finds New Moon authors who live in states that come alphabetically after Massachusetts, where New Moon Books is located.USE pubsSELECT p.pub_name, p.state, a.au_lname, a.au_fname, a.stateFROM publishers p INNER JOIN authors a ON a.state > p.stateWHERE p.pub_name = 'New Moon Books'ORDER BY au_lname ASC, au_fname ASCHere is the result set:pub_name state au_lname au_fname state ---------------- ------- -------------------- -------------------- ----- New Moon Books MA Blotchet-Halls Reginald ORNew Moon Books MA del Castillo Innes MINew Moon Books MA Greene Morningstar TNNew Moon Books MA Panteley Sylvia MDNew Moon Books MA Ringer Albert UTNew Moon Books MA Ringer Anne UT(6 row(s) affected)Joins Using the Not-equal OperatorThe not-equal join (< >) is rarely used. As a general rule, not-equal joins make sense only when used with a self-join. For example, this not-equal Transact-SQL join and self-join are used to find the categories with two or more inexpensive (less than $15) books of different prices:USE pubsSELECT DISTINCT t1.type, t1.priceFROM titles t1 INNER JOIN titles t2 ON t1.type = t2.type AND t1.price <> t2.priceWHERE t1.price < $15 AND t2.price < $15Note The expression NOT column_name = column_name is equivalent to column_name < > column_name.This Transact-SQL example uses a not-equal join combined with a self-join to find all rows in the titleauthor table in which two or more rows have the same title_id but different au_id numbers (that is, books with more than one author):USE pubsSELECT DISTINCT t1.au_id, t1.title_idFROM titleauthor t1 INNER JOIN titleauthor t2 ON t1.title_id = t2.title_idWHERE t1.au_id <> t2.au_idORDER BY t1.au_idHere is the result set:au_id title_id----------- --------213-46-8915 BU1032267-41-2394 BU1111267-41-2394 TC7777409-56-7008 BU1032427-17-2319 PC8888472-27-2349 TC7777672-71-3249 TC7777722-51-5454 MC3021724-80-9391 BU1111724-80-9391 PS1372756-30-7391 PS1372846-92-7186 PC8888899-46-2035 MC3021899-46-2035 PS2091998-72-3567 PS2091(15 row(s) affected)
--------------------keeping it simple... |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-09-28 : 05:22:34
|
If you doSELECT *FROM MainTable AS MT JOIN ChildTable1 AS C1 ON C1.MyPK = MT.MyPK JOIN ChildTable2 AS C2 ON C2.MyPK = MT.MyPK every row in C1 will be joined to every row in C2, which is presumably NOT what you want?How are you planning to display this?If you do a "grid" like:MT.MyPK, MT.Col1, MT.Col2, C1.Col1, C1.Col2, C2.Col1, C2.Col2then its a bit hard to see how you could have flowing data for the C1 and C2 columns in the "grid"If you do<h1>MainTable</h1>PK: MT.MyPKCol1: MT.Col1Col2: MT.Col2<h1>Child Table 1</h1>C1.Col1, C1.Col2... repeat ...<h1>Child Table 2</h1>C2.Col1, C2.Col2... repeat ...then you need separate record sets, and the Presentation Layer to handle walking round the various recordsetsKristen |
 |
|
|
|
|
|
|
|