9.1. Using SELECT to Retrieve Data


9.1. Using SELECT to Retrieve Data

The SELECT statement retrieves information from one or more tables. Retrievals tend to be the most common database operation, so it's important to understand how SELECT works and what you can do with it.

This chapter provides general instructions on how to write SELECT statements and how to use the various parts of its syntax to get the results you want. A representative syntax for the SELECT statement is as follows:

 SELECT values_to_display     FROM table_name     WHERE expression     GROUP BY how_to_group     HAVING expression     ORDER BY how_to_sort     LIMIT row_count; 

The syntax shown here is simplified from the full SELECT syntax, which includes additional clauses that aren't covered in this chapter.

All clauses following the output column list (values_to_display) are optional. For example, you don't need to include a LIMIT clause when writing a SELECT statement. However, any clauses that you do include must be specified in the order shown.

The examples in this chapter use SELECT statements for retrievals involving no more than a single table, but it's possible to retrieve records from more than one table in a single query. One way is by selecting records from one table after the other with multiple SELECT statements and concatenating the results using the UNION keyword. UNION is covered in Section 9.6, "Using UNION." Other multiple-table queries use joins and subqueries, which are covered in later chapters.

In most cases, the sample queries shown here assume that you've already selected a default database. If that isn't true, you can select a database named db_name by issuing a USE db_name statement. For example, select the world database like this:

 mysql> USE world; Database changed 



MySQL 5 Certification Study Guide
MySQL 5.0 Certification Study Guide
ISBN: 0672328127
EAN: 2147483647
Year: 2006
Pages: 312

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net