Database Reference
In-Depth Information
We can set the order of any column the way we wish using
ORDER BY
, and even
specify multiple columns, separating each column with a comma just as we do in the
<what>
section.
We can also use pattern matching to select rows when we only know some of the
information. For example, suppose we want to look up an employee whose name
begins with the letters
McK
but we don't remember the rest. We can look up this
employee using a
WHERE
clause as follows:
MariaDB [test]> SELECT * FROM employees
-> WHERE surname LIKE "McK%";
+----+---------+-----------+-----------+----------+
| id | surname | givenname | pref_name | birthday |
+----+---------+-----------+-----------+----------+
| 5 | McKay | NULL | NULL | NULL |
+----+---------+-----------+-----------+----------+
1 row in set (0.00 sec)
The percent sign (
%
) is what is known as a
wildcard character
. It matches zero or
more instances of any character or group of characters. So by specifying the surname
pattern
McK%
, we are saying that we want any surnames that begin with those letters
and are followed by zero or more other characters. This pattern would match items
such as
McKay
,
McKinsey
,
McKool
, and even
McK
.
Full documentation of the
SELECT
command is found at
https://mariadb.com/kb/
en/select/
.
The
SELECT
commands are even more powerful when we start using
JOINS
to gather
data from multiple tables. For example, if we had an address table along with our
employee table, we could
SELECT
the names and addresses of all employees who live
in a certain city and whose birthdays are less than a month away so that we can send
them a gift certificate to a restaurant in that city. Full documentation of
JOIN
syntax
is found at
https://mariadb.com/kb/en/join/