Specifying Which Rows to Select
Problem
You want to see only those rows that match certain criteria.
Solution
To specify which rows to return, add a WHERE clause to identify the rows that you
want to see, such as customers that live in a particular city or tasks
that have a status of “finished.”
Discussion
Unless you qualify or restrict a SELECT query in some way, it retrieves every
row in your table, which in many cases is a lot more information than
you really want to see. To be more precise about which rows to select,
provide a WHERE clause that
specifies one or more conditions that rows must match.
Conditions can perform tests for equality, inequality, or
relative ordering. For some types of data, such as strings, you can
use pattern matches. The following statements select columns from rows
from the mail table containing
srchost values that are exactly
equal to the string 'venus' or that
begin with the letter 's':
mysql>SELECT t, srcuser, srchost FROM mail WHERE srchost = 'venus';+---------------------+---------+---------+ | t | srcuser | srchost | +---------------------+---------+---------+ | 2006-05-14 09:31:37 | gene | venus | | 2006-05-14 14:42:21 | barb | venus | | 2006-05-15 08:50:57 | phil | venus | | 2006-05-16 09:00:28 | gene | venus | | 2006-05-16 23:04:19 | phil | venus | +---------------------+---------+---------+ mysql>SELECT t, srcuser, srchost FROM mail WHERE srchost LIKE 's%';+---------------------+---------+---------+ | t | srcuser | srchost | +---------------------+---------+---------+ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access