Using Subqueries

Arguably the greatest of all the enhancements to MySQL in version 4.1 is the availability to use subqueries. Using subqueries allows you to execute a query to obtain a result set, and then use these results as a condition to another query—all within the same statement. In other words, you have two statements in your single query, the second valid only with the results of the first.

Think of times when you may have run into a situation in your programming when you wanted to get results from multiple tables, but did not want to join the tables due to their complexity or perhaps they weren't fully relational. Your solution may have been to execute the first query and then loop through the results and perform another query. There's ...

Get Sams Teach Yourself PHP, MySQL® and Apache All in One now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.