Вы находитесь на странице: 1из 8

Laboratory Activity 4

Connecting Tables in MySQL using Query Browser


Objectives
At the end of the activity, student is expected to perform/identify the following:

 Connect multiple tables using WHERE clause;


 Use JOIN clauses;
 Identify the different types of JOIN clauses;
 Connect tables using LEFT JOIN and RIGHT JOIN clauses.

This is a continuation of Laboratory Activity 3. In order to gain full understanding


in-depth, we suggest that you do the said lab activity first before proceeding on this activity.
You may visit it at http://thrivingandliving.blogspot.com/2009/12/mysql-comparison-
operators.html.

On this exercise, we will introduce to new tables at my_store database. These are
ORDERS and ORDERDETAILS. Figure 45 and 46 shows the structure of these tables,
respectively:

Figure 45. ORDERS and ORDERDETAILS Table Structure

Page 1 of 8 http://ThrivingAndLiving.blogspot.com
Take note of the two primary keys set on ORDERDETAILS. You can easily set this
up by altering the indices tab of MySQL Table Editor.

(a)

(b)

Figure 46. ORDERS (a) and ORDERDETAILS (b) data.

Connecting tables using WHERE clause

Let us connect the ORDERDETAILS and PRODUCTS table so we may have a view of
Figure 47. We use the following statement:

SELECT orderdetails.orderID, products.productID,


products.description, products.unit, orderdetails.quantity
FROM orderdetails, products
WHERE orderdetails.productID= products.productID;

Figure 47. Connecting tables using WHERE clause

Page 2 of 8 http://ThrivingAndLiving.blogspot.com
The above statement have used the table names prior to the column names separated
by a dot [ . ] . This is useful to distinguish on which column you need to display especially if
identical column names exists between tables. On the both tables, we have one identical
column name: productID.

You may drop the table name from column names if you are sure that there is no
identical column names between tables; hence, a syntax error will be notified because of the
conflict. But by practice, it is helpful that you identify the table name on where the column
originates.

Sometimes it is quite cumbersome to type the whole table names. An alias will
provide a relief in typing a long statement. Let us convert the last SELECT statement with
an equivalent below:

SELECT o.orderID, p.productID, p.description, p.unit, o.quantity


FROM orderdetails o, products p
WHERE o.productID= p.productID;

o is the alias of orderdetails; p is the alias of products. Declaration of these aliases are
done after the FROM table name specification.

Using JOIN clauses to Connect Tables

You can create a multitable Recordset with a query that uses a JOIN clause. There are two
types of JOIN, and these two JOIN types are implemented by three different possible JOIN
clause types. These types of JOIN and the clauses that implement them are as follows:

 An equi-join or inner join. This type of join creates records in a result set only when
there are matching records from both tables. You can use an INNER JOIN clause to
create an equi-join.

Connecting of two tables using WHERE clause has the same effect for INNER JOIN.
JOIN clause without INNER means the same thing.

 An outer join. Result sets created using this type of join contain all the records from
a specified master table and only those records from a related lookup table that match
the records in the master table. You can implement an outer join with either the LEFT
JOIN or RIGHT JOIN clause. The difference between these two types of join is the
order in which you specify the master and lookup tables.

Page 3 of 8 http://ThrivingAndLiving.blogspot.com
Let us provide an equivalent statement for joining tables with WHERE:

SELECT o.orderID, p.productID, p.description, p.unit, o.quantity


FROM orderdetails o JOIN products p
ON o.productID=p.productID;

The above statement still uses aliases with the distinction of using a JOIN clause with
ON.

How about if we want to check from customers who made their orders. We need to
track orderIDs based on customerNumber. Try to execute the statement below:

SELECT o.orderID, c.customerNo, c.name, c.address


FROM customers c LEFT JOIN orders o
ON c.customerNo= o.customerNo;

Figure 48 has this output:

Figure 48. Using LEFT JOIN clause

From the query results ,you will see the presence of the null value. As we reference
from our master table (customers) to our lookup table (orders), we can note that customers
with null value did not place order yet.

We can achieve the same result using RIGHT JOIN with the revised statement below:

SELECT o.orderID, c.customerNo, c.name, c.address


FROM orders o RIGHT JOIN customers c
ON c.customerNo= o.customerNo;

Page 4 of 8 http://ThrivingAndLiving.blogspot.com
Working on Multiple JOINs

We may issue a statement that derives data from more than two tables. Let us assume that
we need to populate orderID, customerNo, Name, productID, description, unit and quantity
on one single SELECT statement. The statement below does the trick and Figure 49 displays
the output:

SELECT o.orderID, c.customerNo, c. Name, p.productID, p. description, p.unit, od.quantity


FROM customers c LEFT JOIN orders o ON c.customerNo = o.customerNo
LEFT JOIN orderdetails od ON od.orderid=o.orderID
LEFT JOIN products p ON p.productID=od.productID;

Figure 49. Multiple JOINs in a single SELECT statement

For the sake of discussion, the statement above was intentionally indented to simplify
understanding on how multiple tables were connected. We need to understand that
connection between customers and orders must be connected first so we can derive
customerNo, name with common customerNo on both tables.

The resultset of connection between customers and orders may be LEFT JOINed with
orderdetails thru orderID to display quantity column.

Again, the resultset of connecting the first three tables may be LEFT JOINed with
products thru the use of productID to display description and unit.

We may want to improve the display of our output. Instead of displaying all the
customers, our concern can be focused on customers who placed orders. Revising our
statement produces Figure 50.

Page 5 of 8 http://ThrivingAndLiving.blogspot.com
SELECT o.orderID, c.customerNo, c. Name, p.productID, p. description, p.unit, od.quantity
FROM customers c LEFT JOIN orders o ON c.customerNo = o.customerNo
LEFT JOIN orderdetails od ON od.orderid=o.orderID
LEFT JOIN products p ON p.productID=od.productID;
WHERE o.orderID IS NOT NULL;

Figure 50. Filtered data with no null values

The statement above filtered resultset all orderID not having a null value. The output
gives us a clearer view of customers who made the orders.

If we need to count how many products we actually placed on each orderID, we type
the following statement:

SELECT o.orderID, c.customerNo, c.Name, COUNT(od.productID) 'Product Count'


FROM customers c JOIN orders o ON c.customerNo = o.customerNo
LEFT JOIN orderdetails od ON od.orderID=o.orderID
GROUP BY o.orderID;

Figure 51 has the output.

Figure 51. Use of GROUP BY with JOIN clause

As you have noticed, JOIN was used instead of LEFT JOIN between customers and
orders. This change signifies that we need to include only customerNo that are present on
both tables. We connected with orderDetails so we may include productID on which we
will be our basis in counting the products ordered. We used an aggregate function COUNT()
to do this with an alias/label of ‘Product Count’.

Page 6 of 8 http://ThrivingAndLiving.blogspot.com
CHECK YOUR UNDERSTANDING
Do the following tasks:

1. Populate customers who placed order after 12/30/2009.

Figure 52. Output to Item 1

2. List orders placed between 2010-01-05 and 2010-01-06.

Figure 53. Output to Item 2

3. Display summary of products where quantity of orders are listed.

Figure 54. Output for Item 3

Page 7 of 8 http://ThrivingAndLiving.blogspot.com
4. Display only products where quantity ordered is not zero.

Figure 53. Output for Item 4.

5. Display name of customers and products ordered for the date 12/30/2009 and
01/05/2010.

Figure 54. Output for Item 5.

Page 8 of 8 http://ThrivingAndLiving.blogspot.com

Вам также может понравиться