Databases Reference
In-Depth Information
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
+-----------+-----------+
6 rows in set (0.06 sec)
Even though this appears pointless, it can be useful when combined with
aliases
in
more advanced queries, as we show in Chapter 7.
You can specify databases, tables, and column names in a
SELECT
statement. This allows
you to avoid the
USE
command and work with any database and table directly with
SELECT
; it also helps resolve ambiguities, as we show later in “Joining Two Tables.”
Consider an example: suppose you want to retrieve the
album_name
column from the
album
table in the
music
database. You can do this with the following command:
mysql>
SELECT album_name FROM music.album;
+------------------------------------------+
| album_name |
+------------------------------------------+
| Let Love In |
| Retro - John McCready FAN |
| Substance (Disc 2) |
| Retro - Miranda Sawyer POP |
| Retro - New Order / Bobby Gillespie LIVE |
| Live Around The World |
| In A Silent Way |
| Power, Corruption & Lies |
| Exile On Main Street |
| Substance 1987 (Disc 1) |
| Second Coming |
| Light Years |
| Brotherhood |
+------------------------------------------+
13 rows in set (0.01 sec)
The
music.album
component after the
FROM
keyword specifies the
music
database and
its
album
table. There's no need to enter
USE music
before running this query. This syntax
can also be used with other SQL statements, including the
UPDATE
,
DELETE
,
INSERT
, and
SHOW
statements we discuss later in this chapter.
Choosing Rows with the WHERE Clause
This section introduces the
WHERE
clause and explains how to use the Boolean operators
to write expressions. You'll see these in most
SELECT
statements, and often in other
statements such as
UPDATE
and
DELETE
; we'll show you examples later in this chapter.
WHERE basics
The
WHERE
clause is a powerful tool that allows you to choose which rows are returned
from a
SELECT
statement. You use it to return rows that match a condition, such as
having a column value that exactly matches a string, a number greater or less than a