13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250 Chapter 10 Working with Your <strong>MySQL</strong> Database<br />

These items are in separate tables because they relate to separate real-world objects.<br />

This is one of the principles of good database design that we described in Chapter 8,<br />

“Designing Your <strong>Web</strong> Database.”<br />

To put this information together in SQL, you must perform an operation called a<br />

join.This simply means joining two or more tables together to follow the relationships<br />

between the data. For example, if you want to see the orders that customer Julie Smith<br />

has placed, you will need to look at the customers table to find Julie’s customerid <strong>and</strong><br />

then at the orders table for orders with that customerid.<br />

Although joins are conceptually simple, they are one of the more subtle <strong>and</strong> complex<br />

parts of SQL. Several different types of joins are implemented in <strong>MySQL</strong>, <strong>and</strong> each is<br />

used for a different purpose.<br />

Simple Two-Table Joins<br />

Let’s begin by looking at some SQL for the query about Julie Smith we just discussed:<br />

select orders.orderid, orders.amount, orders.date<br />

from customers, orders<br />

where customers.name = ‘Julie Smith’<br />

<strong>and</strong> customers.customerid = orders.customerid;<br />

The output of this query is<br />

+---------+--------+------------+<br />

| orderid | amount | date |<br />

+---------+--------+------------+<br />

| 1 | 69.98 | 2007-04-02 |<br />

| 4 | 24.99 | 2007-05-01 |<br />

+---------+--------+------------+<br />

There are a few things to notice here. First, because information from two tables is needed<br />

to answer this query, you must list both tables.<br />

By listing two tables, you also specify a type of join, possibly without knowing it.The<br />

comma between the names of the tables is equivalent to typing INNER JOIN or CROSS<br />

JOIN.This is a type of join sometimes also referred to as a full join, or the Cartesian product<br />

of the tables. It means,“Take the tables listed, <strong>and</strong> make one big table.The big table<br />

should have a row for each possible combination of rows from each of the tables listed,<br />

whether that makes sense or not.” In other words, you get a table, which has every row<br />

from the customers table matched up with every row from the orders table, regardless<br />

of whether a particular customer placed a particular order.<br />

That brute-force approach doesn’t make a lot of sense in most cases. Often what you<br />

want is to see the rows that really do match—that is, the orders placed by a particular<br />

customer matched up with that customer.<br />

You achieve this result by placing a join condition in the WHERE clause.This special type<br />

of conditional statement explains which attributes show the relationship between the<br />

two tables. In this case, the join condition is<br />

customers.customerid = orders.customerid

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!