• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 4. Using SQL > The SELECT Statement

The SELECT Statement

The SELECT statement is something like a Swiss Army knife, at least for getting information out of a database. It has all sorts of attachments you can use to get the exact results you want.

In this chapter we focus on some of the more basic ways to use this powerful tool, but advanced uses include averages, maximum and minimum values, and all kinds of grouping and aggregating functions that are beyond the scope of this book.


  • If you want to learn more about using SQL and using its more powerful functions, take a look at the SQL Visual QuickStart Guide, by Chris Fehily.

Basic Select statements

First, let’s look at the basic form of the SELECT statement:

SELECT some_stuff
FROM a_table
WHERE conditions_are_met;

For readability, we’re showing each part of the SQL command on a separate line. You could also write the entire command on one line. It doesn’t matter which you do, because SQL won’t execute a command until it sees a semicolon.

Let’s look at the statement in more detail. What we call “some_stuff” above would be replaced by the names of one or more fields from a table. As we discussed in Chapter 3, Database Structure, fields are the columns in the database. So if we had been specifying actual field names, we could have selected, say, the customer name and address while leaving out the phone number, even though the phone number is a field in the database as well. In other words, you can select “some_stuff” without having to select all of it.

The entry “a_table” specifies the table you’re using for that particular information. The “conditions_are_met” entry is where you specify which records you’re looking for. For example, to show the name and description of a product with the ID number 4, you would type:

SELECT product_name,
FROM product_catalog
WHERE product_ID = 4;

This statement will return just the name and description of that one product (Figure 4.5).

Figure 4.5. Use the WHERE clause to get a specific record...


  • The WHERE clause is actually optional. If you omit it, you will get every record in the table. You will rarely want to do this, however, because most databases are fairly large.

Requesting multiple records

You can also select multiple records with the SELECT statement. To get the names and descriptions of the first 10 products in the catalog, you’d type the following:

SELECT product_ID, product_name,
FROM product_catalog
WHERE product_ID <=10;

This command returns the first 10 products in the catalog, and includes the product ID number, for reference (Figure 4.6).

Figure 4.6. ...or a group of records.


  • You can display every field from a record by substituting an asterisk (*) for the list of field names. You’ll see this wildcard in action later in this chapter.

The result of a SELECT statement is called a recordset. It is the basic building block Dreamweaver uses to display data from a database. We’ll examine the recordset more closely later in this chapter.

If you want to type your SELECT statements directly in Dreamweaver instead of building them graphically, Dreamweaver lets you do that.

To write SELECT statements in Dreamweaver:
Open a PHP page.

Create a new recordset (see Chapter 2, Using the Data Bindings Panel).

In the Recordset dialog box, click the Advanced button.

Type your SELECT statement into the SQL window (Figure 4.7).

Figure 4.7. To test out a SELECT statement you can type it in the Recordset dialog box.

Click on OK to close the dialog box and add the statement to your page.

Sample Data Files

You can find sample database files like the ones we use in this chapter on the companion Web site for this book at www.peachpit.com/vqp/dreamweaverMX.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint