Joining SQL Tables


When accessing information within database tables, we may sometimes need to look at data from two or more different tables, as we saw in the previous example. There is another useful way to access relational data, using a technique called joining. When we say "joining," we mean that we compare actual fields by their names in different tables, rather than the actual data within them (although when the statement is executed the actual data is compared).

For an example of how we can use joining, let's look back to the previous example. Let's say that we wanted to retrieve a detailed listing of Henry's enemies and not just their names; in the previous example, we would first execute this statement:

SELECT enemy FROM relateenemies WHERE player = 'Henry'

Then, for each name on the retrieved list, we would call the following SQL statement:

SELECT * FROM playerdata WHERE username = '????';       
(where ???? represents each name on the list in turn)

With the joining technique, however, this can be done in a single SQL statement, meaning less code. It will also execute faster (rather than using two statements). Here is the join statement we would require to do this:

mysql> SELECT * FROM playerdata, relateenemies WHERE      
playerdata.username = relateenemies.enemy AND      
relateenemies.player = 'Henry';

So first we are selecting all the data from both the playerdata and relateenemies tables and then placing a condition upon the joining of the tables, so that data will only be selected where the username field in the playerdata table is equal to the enemy field in the relateenemies table. If we just left it with the single condition, it would return the full results from both tables for each enemy in the relateenemies table. So our second condition limits the results to only show Henry's enemies by comparing the player field in the relateenemies table to the string Henry. Useful, eh?

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: George Ionescu at 12122007

Related Articles

1. Relational SQL Databases
Think of a database that related the players in a game to one another (for example, to determine who was a friend of each player and who was an enemy of each player)....

2. SQL Data Import Methods
This article presents the most common ways of importing data to MySQL. Importing from a Text File To create a text file that contains several re...

3. Basic SQL syntax: Creating and Modifying tables and databases
This tutorial shows basic syntax and commands and of the SQL language. Creating and Dropping Databases First, let's see what databases already exist ...

4. Introduction to SQL
SQL is an acronym for Structured Query Language and is the standard language for interaction with databases. SQL is both an ISO (International Organization for Standard...

5. What Is a Database
A database is simply an organized collection of information. It allows many different types of data to be stored and retrieved in a highly efficient manner. Info...

6. Installing MySQL Server
Installing MySQL is relatively painless. First, you need the installation program for MySQL, which is available on the companion CD. You can also download the latest ve...

7. Data Entry and the Various Industries that use this Service
Group of people flooding Excel sheets with alpha and numeric characters... spontaneous sound of pressing keyboards... these are what you will find in data entry service. Fr...

8. Different Forms and Variations of Data Entry
Data entry is the procedure of feeding data into spreadsheets and database. It can either be done automatically by a machine that keeps on adding data in the database, or m...

9. How Data Entry Services Benefit Industries And Why It Is Essential
It is irrelevant whether you have a small business or a global empire - information is an asset in any kind of business. And when it is business, everything comes down to p...