Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 15 votes, 3.73 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-20-2002, 11:19 AM   PM User | #1
nitric
New Coder

 
Join Date: Jun 2002
Location: Johannesburg, South Africa
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
nitric is an unknown quantity at this point
Post SQL - Joining more than 2 tables

Hi all

Is it possible to join more than 2 tables using SQL? I've used the COLOR=blue]INNER JOIN[/COLOR] statement to join 2 tables based on a common field, but can't get it to work with 3 tables.

Thanks!!
nitric is offline   Reply With Quote
Old 06-20-2002, 01:52 PM   PM User | #2
ReyN
New Coder

 
Join Date: Jun 2002
Location: Pilipinas
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
ReyN is an unknown quantity at this point
Hi

The Sql UNION operation creates a union query, which combines the results of two or more independent queries or tables.

[TABLE] query1 UNION [ALL] [TABLE] query2 [UNION [ALL] [TABLE] queryn [ ... ]]

where query1~n is a SELECT statement, the name of a stored query, or the name of a stored table preceded by the TABLE keyword.

You can merge the results of two or more queries, tables, and SELECT statements, in any combination, using a single UNION statement. The following example merges an existing table named New Accounts and a SELECT statement:

TABLE [New Accounts] UNION ALL
SELECT * FROM Customers
WHERE OrderAmount > 1000;

By default, no duplicate records are returned when you use UNION; however, you can include the ALL predicate to ensure that all records are returned. This also makes the query run faster.

All queries in a UNION operation must request the same number of fields; however, the fields do not have to be of the same size or data type.
__________________
aspxtreme
ReyN is offline   Reply With Quote
Old 06-20-2002, 01:57 PM   PM User | #3
allida77
Regular Coder

 
Join Date: Jun 2002
Location: Cincinnati, OH
Posts: 545
Thanks: 0
Thanked 0 Times in 0 Posts
allida77 is an unknown quantity at this point
http://www.w3schools.com/sql/sql_join.asp
allida77 is offline   Reply With Quote
Old 06-20-2002, 02:15 PM   PM User | #4
ReyN
New Coder

 
Join Date: Jun 2002
Location: Pilipinas
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
ReyN is an unknown quantity at this point
I didn't see any example of joining more than 2 tables in that w3schools link.
__________________
aspxtreme
ReyN is offline   Reply With Quote
Old 06-20-2002, 02:42 PM   PM User | #5
allida77
Regular Coder

 
Join Date: Jun 2002
Location: Cincinnati, OH
Posts: 545
Thanks: 0
Thanked 0 Times in 0 Posts
allida77 is an unknown quantity at this point
Sorry... Can you post your entire SQL statement.
allida77 is offline   Reply With Quote
Old 06-25-2002, 06:06 AM   PM User | #6
ritap
New Coder

 
Join Date: Jun 2002
Location: Ohio
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
ritap is an unknown quantity at this point
If you have 3 tables with the following variables
table1 : ssn, name
table2: ssn, fav_color
table3: ssn, fav_food


SELECT table1.name, table2.fav_color, table3.fav_food
FROM table1, table2, table3
WHERE table1.ssn = table2.ssn
and table2.ssn = table3.ssn

If you wanted to retrieve people(rows) that did not have entries in all 3 tables then you must do outer joins. The syntax is different depending on the database you are using but for Oracle it would be:
SELECT table1.name, table2.fav_color, table3.fav_food
FROM table1, table2, table3
WHERE table1.ssn = table2.ssn(+)
and table2.ssn = table3.ssn(+)
and table3.ssn = tabl1.snn(+)

I hope this helps (and I hope I did not make any typos).
Rita
ritap is offline   Reply With Quote
Old 06-05-2011, 10:01 PM   PM User | #7
BennoM1984
New to the CF scene

 
Join Date: Jun 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
BennoM1984 is an unknown quantity at this point
@allida77 the link is brilliant

thank you !!
BennoM1984 is offline   Reply With Quote
Old 02-29-2012, 03:27 AM   PM User | #8
Vadim Slutsky
New to the CF scene

 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Vadim Slutsky is an unknown quantity at this point
How to join more then two tables :

SELECT
table1.colum1,table2.colum1,table3.column1
FROM table1
INNER JOIN table2 ON (table1.column1=table2.column1)
JOIN table3 ON (table2.column1=table3.column1)
GROUP BY table1.column2;
Vadim Slutsky is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:53 PM.


Advertisement
Log in to turn off these ads.