Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: sql join


Message #1 by "ann win" <awin@a...> on Sat, 5 May 2001 21:11:25
Hi Ann,

You need to have a connection (or 'relationship') between your two 
tables.  This can be done by introducing a new column (or choosing an 
existing one) that is common to both tables.

So in your example, you may have:

[NamesTable]
Name     socialSecurityNo
=======  ===============
name1	 ssn1
name2	 ssn2

[IDsTable]
id         socialSecurityNo
=====      ===============
id1        ssn1
id2        ssn2

Here, you're using the 'socialSecurityNo.' to act as your relationship 
between the two tables as it is common to both.  You now have a way of 
JOINing the tables!  You can then setup your join by saying:

SELECT Name, id 
FROM NamesTable INNER JOIN IDsTable 
ON NamesTable.socialSecurityNo = IDsTable.socialSecurityNo.

The first line is specifying the information you want to select, i.e. you 
may want the firstName and the ID number.

The second line is specifying the tables where the information can be 
found i.e. the NamesTable and the IDsTable respectively. 

The third line is saying how you want to join the information.  You want 
to join the tables where the socialSecurityNo's are the same.

I know all this can be quite puzzling but it's quite simple once you get 
the hang of it!  I recommend you read the excellent "SQL Queries for Mere 
Mortals" by Michael Hernandez and John Viescas (ISBN: 0201433362) - it 
explains all the above brilliantly and will get you writing advanced 
queries in no time.

Good luck,

Jake



  Return to Index