You’ve seen the simplest form of this command getting all the data in a table, like this: SELECT * FROM hero; Figure 9.19 shows this form of the SELECTstatement operating on the herotabl
Trang 1You might want to get a listing of all heroes in your database whose last name
begins with an E, or perhaps somebody parked a Yak Dirigible in your parking
space and need to know who the driver is You may also want your list sorted by
special power or list only vehicles All these (admittedly contrived) examples
involve grabbing a subset of the original data The SELECTstatement is the SQL
workhorse
1 Click the SQL tab to get a query screen in phpMyAdmin
2 Type in a query
3 Click the Go button to see the query results
You’ve seen the simplest form of this command getting all the data in a table,
like this:
SELECT * FROM hero;
Figure 9.19 shows this form of the SELECTstatement operating on the herotable
phpMyAdmin is a wonderful tool for experimenting with SELECT statements because you can write the actual SQL by hand and see immediate results in a very clean format If you don’t want to (or cannot) use phpMyAdmin, do the same exper-iments directly in MySQL It will work, but the results are formatted as text and not always as easy to see.
T R I C K
323
i n
FIGURE 9.19
The SELECT query
is in the top section
and the results are
shown underneath.
Trang 2The SELECTstatement is extremely powerful because it can grab a subset of data that can return only the requested fields and records This process of asking ques-tions of the database is commonly called a query Note that phpMyAdmin some-times adds elements to the query (notably the limitinformation) This increases the query’s efficiency, but doesn’t substantially change the query
Limiting Columns
You might not want all of the fields in a table For example, you might just want
a list of the name and weapon of everyone on your list You can specify this by using the following SELECTstatement, which is illustrated in Figure 9.20:
SELECT name, weapon
FROM hero;
This may seem like a silly capability for such a simple database as the hero list However, but you often run into extremely complicated tables with many fields and need to filter only a few fields For example, I use a database to track student advisees Each student’s information contains lots of data, but I might just want
a list of names and e-mail addresses The ability to isolate the fields I need is one way to get useful information from a database
The results of a query look a lot like a new table You can think of a query result
as a temporary table
324
g r
s o
l u
g in
e r
FIGURE 9.20
This query returns
only the names
and weapons.
Trang 3Limiting Rows with the WHERE Clause
In addition to limiting the columns returned in a query, you may be interested
in limiting the number of rows For example, you might run across an evil villain
who can only be defeated by a laser pointer The query shown in Figure 9.21
illus-trates a query that solves exactly this dilemma
This code returns only the rows matching a specific condition:
SELECT *
FROM hero
WHERE weapon = ‘Laser Pointer’;
Adding a Condition with a WHERE Clause
A WHEREstatement in a query specifies which row(s) you want to see This clause
allows you to specify a condition The database manager checks every record in
the table If the condition is TRUEfor that record, it is included in the result set
The conditions in a WHEREclause are similar to those in PHP code, but they are not
exactlythe same Use these symbols in SQL:
• For equality use the single equal sign (=)
• Encase text elements in single quotation marks (‘)
• Use <, >, and <=or >=and !=conditions to limit your search
325
i n
FIGURE 9.21
If you know how to
set up the query,
you can get very
specific results
In this case, the
query selects only
those heroes with
a laser pointer.
Trang 4Comparison operators are easy to understand for numeric data, such as integers and real numbers It’s not quite so obvious how a language will treat text compar-isons SQL has developed some standard rules, but each implementation might be somewhat different SQL generally works in a case-insensitive way, so Yak-Bot would match yak-bot or yAK-bOT Also, the < and > operators refer to alphabetic order, so the following selects all the records where the hero’s name starts with
A, B, or C.
SELECT *
FROM hero
WHERE name < ‘D’;
Using the LIKE Clause for Partial Matches
Often you do not know the exact value of a field you are trying to match The LIKE clause allows you to specify partial matches For example, which heroes have some sort of super power? This query returns each hero whose power begins with the value Super:
SELECT *
FROM hero
WHERE power LIKE ‘Super%’;
The percent sign (%) can be a wild card, which indicates any character, any num-ber of times You can use a variation of the LIKEclause to find information about all heroes with a transportation scheme that starts with the letter B:
SELECT name, transportation
FROM hero
WHERE transportation LIKE ‘B%’;
You can also use the underscore character (_) to specify one character
The simple wildcard character support in SQL is sufficient for many purposes
If you like regular expressions, you can use the REGEXP clause to specify whether
a field matches a regular expression This is a very powerful tool, but it is an extension to the SQL standard It works fine in MySQL, but it is not supported in all SQL databases.
Generating Multiple Conditions
You can combine conditions with AND, OR, and NOT keywords for more-complex expressions For example, the following code selects those heroes whose trans-portation starts with B and who have a power with superin its name
T R I C K
T R I C K
326
g r
s o
l u
g in
e r
Trang 5SELECT *
FROM hero
WHERE transportation LIKE ‘B%’
AND power LIKE ‘%super%’;
Creating compound expressions is very useful as you build more-complex
data-bases with multiple tables
Sorting Results with the ORDER BY Clause
One more nifty SELECTstatement feature is the ability to sort results by any field
Figures 9.22 and 9.23 illustrate how the ORDER BYclause can determine how tables
are sorted
The ORDER BYclause allows you to determine how the data is sorted You can
spec-ify any field you wish as the sorting field As you can see in Figure 9.23, the DESC
clause specifies that data should be sorted in descending order
Changing Data with the UPDATE Statement
You can use SQL to modify the data in a database The key to this behavior is the
UPDATEstatement An example helps it make sense:
UPDATE hero
SET power = ‘Super Electric Toe’
WHERE name = ‘Lightning Guardian’;
327
i n
FIGURE 9.22
This query shows
the entire database
sorted by the
weapon name.