25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

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

Database Programming ” 151<br />

The recordset returned by this SELECT statement will contain all books written by the<br />

author specified in the WHERE clause (assuming, of course, that your naming convention<br />

is consistent). You may also list more than one parameter in a WHERE clause to<br />

further limit or broaden the results, using a number of logical conjunctions:<br />

SELECT * FROM book<br />

WHERE author = ’Ray Bradbury’ OR author = ’George Orwell’;<br />

SELECT * FROM book<br />

WHERE author = ’Ray Bradbury’ AND publisher LIKE ’%Del Ray’;<br />

The first example statement contains an OR clause and, thus, broadens the results<br />

to return all books by each author, while the second statement further restricts the<br />

results with an AND clause to all books by the author that were also published by a<br />

specific publisher. Note, here, the use of the LIKE operator, which provides a caseinsensitive<br />

match and allows the use of the % wild character to indicate an arbitrary<br />

number of characters. Thus, the expression AND publisher LIKE ’%Del Ray’<br />

will match any publisher that ends in the string del ray, regardless of case.<br />

SQL Joins<br />

As the name implies, joins combine data from multiple tables to create a single<br />

recordset. Many applications use extremely complex joins to return recordsets of<br />

data spanning across many different tables. Some of these joins use subqueries<br />

that contain even more joins nested within them. Since joins often comprise very<br />

complex queries, they are regarded as an advanced SQL concept and many inexperienced<br />

developers try to avoid them—for better or worse. However, they are not quite<br />

as complicated as they are made out to be.<br />

There are two basic types of joins: inner joins and outer joins. In both cases, joins<br />

create a link between two tables based on a common set of columns (keys). An inner<br />

join returns rows from both tables only if keys from both tables can be found that<br />

satisfies the join conditions. For example:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

SELECT *<br />

FROM book INNER JOIN book_chapter<br />

ON book.isbn = book_chapter.isbn;

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

Saved successfully!

Ooh no, something went wrong!