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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Inner Joins

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.
Go to Top of Page

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
Go to Top of Page

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 ?.
Go to Top of Page

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 Joins
An 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 pubs
SELECT *
FROM authors AS a INNER JOIN publishers AS p
ON a.city = p.city
ORDER BY a.au_lname DESC

This 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. Berkeley
409-56-7008 Bennet Abraham 415 658-9932 6223 Bateman St. Berkeley

state 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 pubs
SELECT p.pub_id, p.pub_name, p.state, a.*
FROM publishers p INNER JOIN authors a
ON p.city = a.city
ORDER BY a.au_lname ASC, a.au_fname ASC

Here is the result set:

pub_id pub_name state au_id au_lname au_fname
------ --------------- -------- ----------- -------- -------- 1389 Algodata Infosystems CA 409-56-7008 Bennet Abraham
1389 Algodata Infosystems CA 238-95-7766 Carson Cheryl

phone address city state zip contract
--------------- ------------- -------- ----- ----- ---------
415 658-9932 6223 Bateman St. Berkeley CA 94705 1
415 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 Equal
You 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 pubs
SELECT p.pub_name, p.state, a.au_lname, a.au_fname, a.state
FROM publishers p INNER JOIN authors a
ON a.state > p.state
WHERE p.pub_name = 'New Moon Books'
ORDER BY au_lname ASC, au_fname ASC

Here is the result set:

pub_name state au_lname au_fname state
---------------- ------- -------------------- -------------------- -----
New Moon Books MA Blotchet-Halls Reginald OR
New Moon Books MA del Castillo Innes MI
New Moon Books MA Greene Morningstar TN
New Moon Books MA Panteley Sylvia MD
New Moon Books MA Ringer Albert UT
New Moon Books MA Ringer Anne UT

(6 row(s) affected)

Joins Using the Not-equal Operator
The 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 pubs
SELECT DISTINCT t1.type, t1.price
FROM titles t1 INNER JOIN titles t2
ON t1.type = t2.type
AND t1.price <> t2.price
WHERE t1.price < $15 AND t2.price < $15



Note 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 pubs
SELECT DISTINCT t1.au_id, t1.title_id
FROM titleauthor t1 INNER JOIN titleauthor t2
ON t1.title_id = t2.title_id
WHERE t1.au_id <> t2.au_id
ORDER BY t1.au_id

Here is the result set:

au_id title_id
----------- --------
213-46-8915 BU1032
267-41-2394 BU1111
267-41-2394 TC7777
409-56-7008 BU1032
427-17-2319 PC8888
472-27-2349 TC7777
672-71-3249 TC7777
722-51-5454 MC3021
724-80-9391 BU1111
724-80-9391 PS1372
756-30-7391 PS1372
846-92-7186 PC8888
899-46-2035 MC3021
899-46-2035 PS2091
998-72-3567 PS2091

(15 row(s) affected)





--------------------
keeping it simple...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-28 : 05:22:34
If you do

SELECT *
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.Col2

then 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.MyPK
Col1: MT.Col1
Col2: 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 recordsets

Kristen
Go to Top of Page
   

- Advertisement -