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.

Retrieving Data from the Database<br />

251<br />

which tells <strong>MySQL</strong> to put rows in the result table only if the customerid from the<br />

customers table matches the customerid from the orders table.<br />

By adding this join condition to the query, you actually convert the join to a different<br />

type, called an equi-join.<br />

Also notice the dot notation used to make it clear which table a particular column<br />

comes from; that is, customers.customerid refers to the customerid column from the<br />

customers table, <strong>and</strong> orders.customerid refers to the customerid column from the<br />

orders table.<br />

This dot notation is required if the name of a column is ambiguous—that is, if it<br />

occurs in more than one table. As an extension, it can also be used to disambiguate column<br />

names from different databases.This example uses a table.column notation, but<br />

you can specify the database with a database.table.column notation, for example, to<br />

test a condition such as<br />

books.orders.customerid = other_db.orders.customerid<br />

You can, however, use the dot notation for all column references in a query. Using this<br />

notation can be a good idea, particularly when your queries begin to become complex.<br />

<strong>MySQL</strong> doesn’t require it, but it does make your queries much more humanly readable<br />

<strong>and</strong> maintainable. Notice that we followed this convention in the rest of the previous<br />

query, for example, with the use of the condition<br />

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

The column name occurs only in the table customers, so we do not really need to specify<br />

what table it is from. <strong>MySQL</strong> will not be confused. For humans, though, the name on<br />

its own is vague, so it does make the meaning of the query clearer when you specify it as<br />

customer.name.<br />

Joining More Than Two Tables<br />

Joining more than two tables is no more difficult than a two-table join. As a general rule,<br />

you need to join tables in pairs with join conditions.Think of it as following the relationships<br />

between the data from table to table to table.<br />

For example, if you want to know which customers have ordered books on Java (perhaps<br />

so you can send them information about a new Java book), you need to trace these<br />

relationships through quite a few tables.<br />

You need to find customers who have placed at least one order that included an<br />

order_item that is a book about Java.To get from the customers table to the orders<br />

table, you can use the customerid as shown previously.To get from the orders table to<br />

the order_items table, you can use the orderid.To get from the order_items table to<br />

the specific book in the Books table, you can use the ISBN. After making all those links,<br />

you can test for books with Java in the title <strong>and</strong> return the names of customers who<br />

bought any of those books.

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

Saved successfully!

Ooh no, something went wrong!