Specifications

You can run this script by piping it through MySQL as follows:
>mysql -h host -u bookorama -p < book_insert.sql
Retrieving Data from the Database
The workhorse of SQL is the SELECT statement. Its used to retrieve data from a database by
selecting rows that match specified criteria from a table. There are a lot of options and differ-
ent ways to use the SELECT statement.
The basic form of a SELECT is
SELECT items
FROM tables
[ WHERE condition ]
[ GROUP BY group_type ]
[ HAVING where_definition ]
[ ORDER BY order_type ]
[LIMIT limit_criteria ] ;
Well talk about each of the clauses of the statement. First of all, though, lets look at a query
without any of the optional clauses, one that selects some items from a particular table.
Typically, these items are columns from the table. (They can also be the results of any MySQL
expressions. Well discuss some of the more useful ones later in this section.) This query lists
the contents of the name and city columns from the Customers table:
select name, city
from customers;
This query has the following output, assuming that youve entered the sample data from
Listing 9.1:
+-----------------+--------------------+
| name | city |
+-----------------+--------------------+
| Julie Smith | Airport West |
| Alan Wong | Box Hill |
| Michelle Arthur | Yarraville |
| Melissa Jones | Nar Nar Goon North |
| Michael Archer | Leeton |
+-----------------+--------------------+
As you can see, weve got a table which contains the items we selectedname and cityfrom
the table we specified, Customers. This data is shown for all the rows in the Customer table.
You can specify as many columns as you like from a table by listing them out after the select
keyword. You can also specify some other items. One useful one is the wildcard operator, *,
Working with Your MySQL Database
C
HAPTER 9
9
W
ORKING WITH
YOUR MYSQL
DATABASE
211
12 7842 CH09 3/6/01 3:36 PM Page 211