Querying data
It took quite a lot of time, but we are finally in the most exciting—and useful—section related to databases: querying data. Querying data refers to asking MySQL to return rows from the specified table and optionally filtering these results by a set of rules. You can also choose to get specific fields instead of the whole row. In order to query data, we will use the SELECT
command, as follows:
mysql> SELECT firstname, surname, type FROM customer; +-----------+---------+---------+ | firstname | surname | type | +-----------+---------+---------+ | Han | Solo | premium | | James | Kirk | basic | +-----------+---------+---------+ 2 rows in set (0.00 sec)
One of the simplest ways to query data is to specify the fields of interest after SELECT
and specify the table with the FROM
keyword. As we did not add any filters—mostly known as conditions—to the query, we got all the rows there. Sometimes, this is the desired behavior, but the most common thing to do...