1. Home
  2. Computing & Technology
  3. PHP / MySQL

Using SQL Joins

By , About.com Guide

1 of 4

Introduction to SQL Joins

You may remember from our previous MySQL Tutorials that a database can consist of many tables, and being a relational database meant we could combine information from different tables. In our beginning examples we have only used one table, however we are now going to explore how to best use multiple tables in MySQL.

Let's go back to our doctor's office scenario. Let's first create a simple table of patients. Execute this code in the query window or at a command line:

CREATE TABLE patients (name VARCHAR(30), age INTEGER, med_id INTEGER); INSERT INTO patients VALUES ( "Jim", 45, 6 ), ( "Peggy", 6, 1 ), ( "Lisa", 25, 4 ), ( "Bert", 66, 2 )

You should now have a table with 4 patients. Each patient has a name, an age and a medicine ID number. Now run this:

CREATE TABLE meds (name VARCHAR(30), dose INTEGER, med_id INTEGER); INSERT INTO meds VALUES ( "PillA", 2, 1 ), ( "PillB", 3, 2 ), ( "PillC", 1, 3 ), ( "PillD", 4, 4 ), ( "PillE", 2, 5 ), ( "PillF", 5, 6 )

You now have a second table called "meds" which holds the names and doses of 6 types of medicine. You will notice that the field "med_id" now appears in both of our tables.

Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn MySQL
  5. SQL Join - SQL Join Statement - SQL Join Type

©2009 About.com, a part of The New York Times Company.

All rights reserved.