• Tidak ada hasil yang ditemukan

Counting and Grouping Results

Dalam dokumen Buku Learning MySQL and MariaDB (Halaman 158-164)

column by which to group. This is where the GROUP BY clause comes in. This clause tells MySQL to group the results based on the columns given with the clause. Let’s see how that might look. Enter the following on your server:

SELECT orders.scientific_name AS 'Order', families.scientific_name AS 'Family', COUNT(*) AS 'Number of Birds'

FROM birds, bird_families AS families, bird_orders AS orders WHERE birds.family_id = families.family_id

AND families.order_id = orders.order_id AND orders.scientific_name = 'Pelecaniformes' GROUP BY Family;

+---+---+---+

| Order | Family | Number of Birds | +---+---+---+

| Pelecaniformes | Ardeidae | 157 |

| Pelecaniformes | Balaenicipitidae | 1 |

| Pelecaniformes | Pelecanidae | 10 |

| Pelecaniformes | Scopidae | 3 |

| Pelecaniformes | Threskiornithidae | 53 | +---+---+---+

We gave the GROUP BY clause the Family alias, which is the scientific_name column from the bird_families table. MySQL returns one results set for all five families, for one

SELECT statement.

The GROUP BY clause is very useful. You’ll use it often, so learn it well. This clause and related functions are covered in greater detail in Chapter 12.

Summary

The SELECT statement offers quite a number of parameters and possibilities that I had to skip to keep this chapter from becoming too lengthy and too advanced for a learning book.

For instance, there are several options for caching results and a clause for exporting a results set to a text file. You can learn about these from other sources if you need them.

At this point, make sure you’re comfortable with the SELECT statement and its main components: choosing columns and using field aliases; choosing multiple tables in the

FROM clause; how to construct a WHERE clause, including the basics of regular expressions;

using the ORDER BY and the GROUP BY clauses; and limiting results with the LIMIT clause.

It will take time and practice to become very comfortable with all of these components.

Before moving on to Chapter 8, make sure to complete the exercises in the next section.

Exercises

The following exercises will help cement your understanding of the SELECT statement. The act of typing SQL statements, especially ones that you will use often like SELECT, helps you to learn, memorize, and know them well.

1. Construct a SELECT statement to select the common names of birds from the birds

table. Use the LIKE operator to select only Pigeons from the table. Order the table by the common_name column, but give it a field alias of Bird'. Don’t limit the results; let MySQL retrieve all of the rows that match. Execute the statement on your server and look over the results.

Next, use the same SELECT statement, but add a LIMIT clause. Limit the results to the first ten rows and execute it. Compare the results to the previous SELECT statement to make sure the results show the 1st through 10th row. Then modify the SELECT

statement again to display the next 10 rows. Compare these results to the results from the first SELECT statement to make sure you retrieved the 11th through 20th row. If you didn’t, find your mistake and correct it until you get it right.

2. In this exercise, you’ll begin with a simple SELECT statement and then make it more complicated. To start, construct a SELECT statement in which you select the

scientific_name and the brief_description from the bird_orders table. Give the field for the scientific_name an alias of Order — and don’t forget to put quotes around it because it’s a reserved word. Use an alias of Types of Birds in Order for

brief_description. Don’t limit the results. When you think that you have the

SELECT statement constructed properly, execute it. If you have errors, try to determine the problem and fix the statement until you get it right.

Construct another SELECT statement in which you retrieve data from the birds table.

Select the common_name and the scientific_name columns. Give them field aliases:

Common Name of Bird and Scientific Name of Bird. Exclude rows in which the

common_name column is blank. Order the data by the common_name column. Limit the results to 25 rows of data. Execute the statement until it works without an error.

Merge the first and second SELECT statements together to form one SELECT statement that retrieves the same four columns with the same alias from the same two tables (this was covered in Combining Tables). It involves giving more than one table in the FROM clause and providing value pairs in the WHERE clause for temporarily

connecting the tables to each other. This one may seem tricky. So take your time and don’t get frustrated. If necessary, refer back to Combining Tables.

Limit the results to 25 rows. If you do it right, you should get the same 25 birds from the second SELECT of this exercise, but with two more fields of data. Be sure to

exclude rows in which the common_name column is blank.

3. Use the SELECT statement in conjunction with REGEXP in the WHERE clause to get a list of birds from the birds table in which the common_name contains the word “Pigeon”

or “Dove” (this was covered in Expressions and the Like). Give the field for the

common_name column the alias >Type of Columbidae — that’s the name of the family to which Doves and Pigeons belong.

Chapter 8. Updating and Deleting Data

Data in databases will change often. There’s always something to change, some bit of information to add, some record to delete. For these situations in which you want to change or add pieces of data, you will mostly use the UPDATE statement. For situations in which you want to delete an entire row of data, you’ll primarily use the DELETE statement.

Both of these SQL statements are covered extensively in this chapter.

Dalam dokumen Buku Learning MySQL and MariaDB (Halaman 158-164)