The
SELECT statement is used to pull information from a table. The general form of the statement is:SELECTwhat_to_selectFROMwhich_tableWHEREconditions_to_satisfy;
what_to_select indicates what you want to see. This can be a list of columns, or * to indicate “all columns.” which_table indicates the table from which you want to retrieve data. The WHERE clause is optional. If it is present, conditions_to_satisfy specifies one or more conditions that rows must satisfy to qualify for retrieval.Selecting All Data
The simplest form of
SELECT retrieves everything from a table:mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+
This form of
SELECT is useful if you want to review your entire table, for example, after you've just loaded it with your initial data set. For example, you may happen to think that the birth date for Bowser doesn't seem quite right. Consulting your original pedigree papers, you find that the correct birth year should be 1989, not 1979.There are at least two ways to fix this:
- Edit the file
pet.txtto correct the error, then empty the table and reload it usingDELETEandLOAD DATA:mysql>
DELETE FROM pet;mysql>LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;However, if you do this, you must also re-enter the record for Puffball. - Fix only the erroneous record with an
UPDATEstatement:mysql>
UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';TheUPDATEchanges only the record in question and does not require you to reload the table.Selecting Particular Rows
As shown in the preceding section, it is easy to retrieve an entire table. Just omit theWHEREclause from theSELECTstatement. But typically you don't want to see the entire table, particularly when it becomes large. Instead, you're usually more interested in answering a particular question, in which case you specify some constraints on the information you want. Let's look at some selection queries in terms of questions about your pets that they answer.You can select only particular rows from your table. For example, if you want to verify the change that you made to Bowser's birth date, select Bowser's record like this:mysql>
SELECT * FROM pet WHERE name = 'Bowser';+--------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+-------+---------+------+------------+------------+ | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+-------+---------+------+------------+------------+The output confirms that the year is correctly recorded as 1989, not 1979.String comparisons normally are case-insensitive, so you can specify the name as'bowser','BOWSER', and so forth. The query result is the same.You can specify conditions on any column, not justname. For example, if you want to know which animals were born during or after 1998, test thebirthcolumn:mysql>
SELECT * FROM pet WHERE birth >= '1998-1-1';+----------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+-------+ | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +----------+-------+---------+------+------------+-------+You can combine conditions, for example, to locate female dogs:mysql>
SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+mysql>
SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';+----------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+-------+ | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+-------+---------+------+------------+-------+ANDandORmay be intermixed, althoughANDhas higher precedence thanOR. If you use both operators, it is a good idea to use parentheses to indicate explicitly how conditions should be grouped:mysql>
SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')->OR (species = 'dog' AND sex = 'f');+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+Selecting Particular Columns
If you do not want to see entire rows from your table, just name the columns in which you are interested, separated by commas. For example, if you want to know when your animals were born, select thenameandbirthcolumns:mysql>
SELECT name, birth FROM pet;+----------+------------+ | name | birth | +----------+------------+ | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | | Fang | 1990-08-27 | | Bowser | 1989-08-31 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Puffball | 1999-03-30 | +----------+------------+To find out who owns pets, use this query:mysql>
SELECT owner FROM pet;+--------+ | owner | +--------+ | Harold | | Gwen | | Harold | | Benny | | Diane | | Gwen | | Gwen | | Benny | | Diane | +--------+Notice that the query simply retrieves theownercolumn from each record, and some of them appear more than once. To minimize the output, retrieve each unique output record just once by adding the keywordDISTINCT:mysql>
SELECT DISTINCT owner FROM pet;+--------+ | owner | +--------+ | Benny | | Diane | | Gwen | | Harold | +--------+You can use aWHEREclause to combine row selection with column selection. For example, to get birth dates for dogs and cats only, use this query:mysql>
SELECT name, species, birth FROM pet->WHERE species = 'dog' OR species = 'cat';+--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | Fluffy | cat | 1993-02-04 | | Claws | cat | 1994-03-17 | | Buffy | dog | 1989-05-13 | | Fang | dog | 1990-08-27 | | Bowser | dog | 1989-08-31 | +--------+---------+------------+Sorting Rows
You may have noticed in the preceding examples that the result rows are displayed in no particular order. It is often easier to examine query output when the rows are sorted in some meaningful way. To sort a result, use anORDER BYclause.Here are animal birthdays, sorted by date:mysql>
SELECT name, birth FROM pet ORDER BY birth;+----------+------------+ | name | birth | +----------+------------+ | Buffy | 1989-05-13 | | Bowser | 1989-08-31 | | Fang | 1990-08-27 | | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | Slim | 1996-04-29 | | Whistler | 1997-12-09 | | Chirpy | 1998-09-11 | | Puffball | 1999-03-30 | +----------+------------+On character type columns, sorting—like all other comparison operations—is normally performed in a case-insensitive fashion. This means that the order is undefined for columns that are identical except for their case. You can force a case-sensitive sort for a column by usingBINARYlike so:ORDER BY BINARY.col_nameThe default sort order is ascending, with smallest values first. To sort in reverse (descending) order, add theDESCkeyword to the name of the column you are sorting by:mysql>
SELECT name, birth FROM pet ORDER BY birth DESC;+----------+------------+ | name | birth | +----------+------------+ | Puffball | 1999-03-30 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Claws | 1994-03-17 | | Fluffy | 1993-02-04 | | Fang | 1990-08-27 | | Bowser | 1989-08-31 | | Buffy | 1989-05-13 | +----------+------------+You can sort on multiple columns, and you can sort different columns in different directions. For example, to sort by type of animal in ascending order, then by birth date within animal type in descending order (youngest animals first), use the following query:mysql>
SELECT name, species, birth FROM pet->ORDER BY species, birth DESC;+----------+---------+------------+ | name | species | birth | +----------+---------+------------+ | Chirpy | bird | 1998-09-11 | | Whistler | bird | 1997-12-09 | | Claws | cat | 1994-03-17 | | Fluffy | cat | 1993-02-04 | | Fang | dog | 1990-08-27 | | Bowser | dog | 1989-08-31 | | Buffy | dog | 1989-05-13 | | Puffball | hamster | 1999-03-30 | | Slim | snake | 1996-04-29 | +----------+---------+------------+TheDESCkeyword applies only to the column name immediately preceding it (birth); it does not affect thespeciescolumn sort order.Date Calculations
MySQL provides several functions that you can use to perform calculations on dates, for example, to calculate ages or extract parts of dates.To determine how many years old each of your pets is, compute the difference in the year part of the current date and the birth date, then subtract one if the current date occurs earlier in the calendar year than the birth date. The following query shows, for each pet, the birth date, the current date, and the age in years.mysql>
SELECT name, birth, CURDATE(),->(YEAR(CURDATE())-YEAR(birth))->- (RIGHT(CURDATE(),5)<RIGHT(birth,5))->AS age->FROM pet;+----------+------------+------------+------+ | name | birth | CURDATE() | age | +----------+------------+------------+------+ | Fluffy | 1993-02-04 | 2003-08-19 | 10 | | Claws | 1994-03-17 | 2003-08-19 | 9 | | Buffy | 1989-05-13 | 2003-08-19 | 14 | | Fang | 1990-08-27 | 2003-08-19 | 12 | | Bowser | 1989-08-31 | 2003-08-19 | 13 | | Chirpy | 1998-09-11 | 2003-08-19 | 4 | | Whistler | 1997-12-09 | 2003-08-19 | 5 | | Slim | 1996-04-29 | 2003-08-19 | 7 | | Puffball | 1999-03-30 | 2003-08-19 | 4 | +----------+------------+------------+------+Here,YEAR()pulls out the year part of a date andRIGHT()pulls off the rightmost five characters that represent theMM-DD(calendar year) part of the date. The part of the expression that compares theMM-DDvalues evaluates to 1 or 0, which adjusts the year difference down a year ifCURDATE()occurs earlier in the year thanbirth. The full expression is somewhat ungainly, so an alias (age) is used to make the output column label more meaningful.The query works, but the result could be scanned more easily if the rows were presented in some order. This can be done by adding anORDER BY nameclause to sort the output by name:mysql>
SELECT name, birth, CURDATE(),->(YEAR(CURDATE())-YEAR(birth))->- (RIGHT(CURDATE(),5)<RIGHT(birth,5))->AS age->FROM pet ORDER BY name;+----------+------------+------------+------+ | name | birth | CURDATE() | age | +----------+------------+------------+------+ | Bowser | 1989-08-31 | 2003-08-19 | 13 | | Buffy | 1989-05-13 | 2003-08-19 | 14 | | Chirpy | 1998-09-11 | 2003-08-19 | 4 | | Claws | 1994-03-17 | 2003-08-19 | 9 | | Fang | 1990-08-27 | 2003-08-19 | 12 | | Fluffy | 1993-02-04 | 2003-08-19 | 10 | | Puffball | 1999-03-30 | 2003-08-19 | 4 | | Slim | 1996-04-29 | 2003-08-19 | 7 | | Whistler | 1997-12-09 | 2003-08-19 | 5 | +----------+------------+------------+------+To sort the output byagerather thanname, just use a differentORDER BYclause:mysql>
SELECT name, birth, CURDATE(),->(YEAR(CURDATE())-YEAR(birth))->- (RIGHT(CURDATE(),5)<RIGHT(birth,5))->AS age->FROM pet ORDER BY age;+----------+------------+------------+------+ | name | birth | CURDATE() | age | +----------+------------+------------+------+ | Chirpy | 1998-09-11 | 2003-08-19 | 4 | | Puffball | 1999-03-30 | 2003-08-19 | 4 | | Whistler | 1997-12-09 | 2003-08-19 | 5 | | Slim | 1996-04-29 | 2003-08-19 | 7 | | Claws | 1994-03-17 | 2003-08-19 | 9 | | Fluffy | 1993-02-04 | 2003-08-19 | 10 | | Fang | 1990-08-27 | 2003-08-19 | 12 | | Bowser | 1989-08-31 | 2003-08-19 | 13 | | Buffy | 1989-05-13 | 2003-08-19 | 14 | +----------+------------+------------+------+A similar query can be used to determine age at death for animals that have died. You determine which animals these are by checking whether thedeathvalue isNULL. Then, for those with non-NULLvalues, compute the difference between thedeathandbirthvalues:mysql>
SELECT name, birth, death,->(YEAR(death)-YEAR(birth)) - (RIGHT(death,5)<RIGHT(birth,5))->AS age->FROM pet WHERE death IS NOT NULL ORDER BY age;+--------+------------+------------+------+ | name | birth | death | age | +--------+------------+------------+------+ | Bowser | 1989-08-31 | 1995-07-29 | 5 | +--------+------------+------------+------+The query usesdeath IS NOT NULLrather thandeath <> NULLbecauseNULLis a special value that cannot be compared using the usual comparison operators. This is discussed later. SeeSection 3.3.4.6, “Working withNULLValues”.What if you want to know which animals have birthdays next month? For this type of calculation, year and day are irrelevant; you simply want to extract the month part of thebirthcolumn. MySQL provides several functions for extracting parts of dates, such asYEAR(),MONTH(), andDAYOFMONTH().MONTH()is the appropriate function here. To see how it works, run a simple query that displays the value of bothbirthandMONTH(birth):mysql>
SELECT name, birth, MONTH(birth) FROM pet;+----------+------------+--------------+ | name | birth | MONTH(birth) | +----------+------------+--------------+ | Fluffy | 1993-02-04 | 2 | | Claws | 1994-03-17 | 3 | | Buffy | 1989-05-13 | 5 | | Fang | 1990-08-27 | 8 | | Bowser | 1989-08-31 | 8 | | Chirpy | 1998-09-11 | 9 | | Whistler | 1997-12-09 | 12 | | Slim | 1996-04-29 | 4 | | Puffball | 1999-03-30 | 3 | +----------+------------+--------------+Finding animals with birthdays in the upcoming month is also simple. Suppose that the current month is April. Then the month value is4and you can look for animals born in May (month5) like this:mysql>
SELECT name, birth FROM pet WHERE MONTH(birth) = 5;+-------+------------+ | name | birth | +-------+------------+ | Buffy | 1989-05-13 | +-------+------------+There is a small complication if the current month is December. You cannot merely add one to the month number (12) and look for animals born in month13, because there is no such month. Instead, you look for animals born in January (month1).You can write the query so that it works no matter what the current month is, so that you do not have to use the number for a particular month.DATE_ADD()enables you to add a time interval to a given date. If you add a month to the value ofCURDATE(), then extract the month part withMONTH(), the result produces the month in which to look for birthdays:mysql>
SELECT name, birth FROM pet->WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));A different way to accomplish the same task is to add1to get the next month after the current one after using the modulo function (MOD) to wrap the month value to0if it is currently12:mysql>
SELECT name, birth FROM pet->WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;MONTH()returns a number between1and12. AndMOD(something,12)returns a number between0and11. So the addition has to be after theMOD(), otherwise we would go from November (11) to January (1).Working with
NULLValuesTheNULLvalue can be surprising until you get used to it. Conceptually,NULLmeans “a missing unknown value” and it is treated somewhat differently from other values.mysql>
SELECT 1 IS NULL, 1 IS NOT NULL;+-----------+---------------+ | 1 IS NULL | 1 IS NOT NULL | +-----------+---------------+ | 0 | 1 | +-----------+---------------+You cannot use arithmetic comparison operators such as=,<, or<>to test forNULL. To demonstrate this for yourself, try the following query:mysql>
SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;+----------+-----------+----------+----------+ | 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL | +----------+-----------+----------+----------+ | NULL | NULL | NULL | NULL | +----------+-----------+----------+----------+Because the result of any arithmetic comparison withNULLis alsoNULL, you cannot obtain any meaningful results from such comparisons.In MySQL,0orNULLmeans false and anything else means true. The default truth value from a boolean operation is1.This special treatment ofNULLis why, in the previous section, it was necessary to determine which animals are no longer alive usingdeath IS NOT NULLinstead ofdeath <> NULL.TwoNULLvalues are regarded as equal in aGROUP BY.When doing anORDER BY,NULLvalues are presented first if you doORDER BY ... ASCand last if you doORDER BY ... DESC.A common error when working withNULLis to assume that it is not possible to insert a zero or an empty string into a column defined asNOT NULL, but this is not the case. These are in fact values, whereasNULLmeans “not having a value.” You can test this easily enough by usingIS [NOT] NULLas shown:mysql>
SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;+-----------+---------------+------------+----------------+ | 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL | +-----------+---------------+------------+----------------+ | 0 | 1 | 0 | 1 | +-----------+---------------+------------+----------------+Thus it is entirely possible to insert a zero or empty string into aNOT NULLcolumn, as these are in factNOT NULL. See Section C.5.5.3, “Problems withNULLValues”.Pattern Matching
MySQL provides standard SQL pattern matching as well as a form of pattern matching based on extended regular expressions similar to those used by Unix utilities such as vi,grep, and sed.SQL pattern matching enables you to use “_” to match any single character and “%” to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. You do not use=or<>when you use SQL patterns; use theLIKEorNOT LIKEcomparison operators instead.To find names beginning with “b”:mysql>
SELECT * FROM pet WHERE name LIKE 'b%';+--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+--------+---------+------+------------+------------+To find names ending with “fy”:mysql>
SELECT * FROM pet WHERE name LIKE '%fy';+--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+To find names containing a “w”:mysql>
SELECT * FROM pet WHERE name LIKE '%w%';+----------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+------------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------+-------+---------+------+------------+------------+To find names containing exactly five characters, use five instances of the “_” pattern character:mysql>
SELECT * FROM pet WHERE name LIKE '_____';+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+The other type of pattern matching provided by MySQL uses extended regular expressions. When you test for a match for this type of pattern, use theREGEXPandNOT REGEXPoperators (orRLIKEandNOT RLIKE, which are synonyms).The following list describes some characteristics of extended regular expressions:- “
.” matches any single character. - A character class “
[...]” matches any character within the brackets. For example, “[abc]” matches “a”, “b”, or “c”. To name a range of characters, use a dash. “[a-z]” matches any letter, whereas “[0-9]” matches any digit. - “
*” matches zero or more instances of the thing preceding it. For example, “x*” matches any number of “x” characters, “[0-9]*” matches any number of digits, and “.*” matches any number of anything. - To anchor a pattern so that it must match the beginning or end of the value being tested, use “
^” at the beginning or “$” at the end of the pattern.
To demonstrate how extended regular expressions work, theLIKEqueries shown previously are rewritten here to useREGEXP.To find names beginning with “b”, use “^” to match the beginning of the name:mysql>
SELECT * FROM pet WHERE name REGEXP '^b';+--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+--------+---------+------+------------+------------+If you really want to force aREGEXPcomparison to be case sensitive, use theBINARYkeyword to make one of the strings a binary string. This query matches only lowercase “b” at the beginning of a name:mysql>
SELECT * FROM pet WHERE name REGEXP BINARY '^b';To find names ending with “fy”, use “$” to match the end of the name:mysql>
SELECT * FROM pet WHERE name REGEXP 'fy$';+--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+To find names containing a “w”, use this query:mysql>
SELECT * FROM pet WHERE name REGEXP 'w';+----------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+------------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------+-------+---------+------+------------+------------+Because a regular expression pattern matches if it occurs anywhere in the value, it is not necessary in the previous query to put a wildcard on either side of the pattern to get it to match the entire value like it would be if you used an SQL pattern.To find names containing exactly five characters, use “^” and “$” to match the beginning and end of the name, and five instances of “.” in between:mysql>
SELECT * FROM pet WHERE name REGEXP '^.....$';+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+You could also write the previous query using the{(“repeat-n}n-times”) operator:mysql>
SELECT * FROM pet WHERE name REGEXP '^.{5}$';+-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+Section 11.5.2, “Regular Expressions”, provides more information about the syntax for regular expressions.Counting Rows
Databases are often used to answer the question, “How often does a certain type of data occur in a table?” For example, you might want to know how many pets you have, or how many pets each owner has, or you might want to perform various kinds of census operations on your animals.Counting the total number of animals you have is the same question as “How many rows are in thepettable?” because there is one record per pet.COUNT(*)counts the number of rows, so the query to count your animals looks like this:mysql>
SELECT COUNT(*) FROM pet;+----------+ | COUNT(*) | +----------+ | 9 | +----------+Earlier, you retrieved the names of the people who owned pets. You can useCOUNT()if you want to find out how many pets each owner has:mysql>
SELECT owner, COUNT(*) FROM pet GROUP BY owner;+--------+----------+ | owner | COUNT(*) | +--------+----------+ | Benny | 2 | | Diane | 2 | | Gwen | 3 | | Harold | 2 | +--------+----------+The preceding query usesGROUP BYto group all records for eachowner. The use ofCOUNT()in conjunction withGROUP BYis useful for characterizing your data under various groupings. The following examples show different ways to perform animal census operations.Number of animals per species:mysql>
SELECT species, COUNT(*) FROM pet GROUP BY species;+---------+----------+ | species | COUNT(*) | +---------+----------+ | bird | 2 | | cat | 2 | | dog | 3 | | hamster | 1 | | snake | 1 | +---------+----------+Number of animals per sex:mysql>
SELECT sex, COUNT(*) FROM pet GROUP BY sex;+------+----------+ | sex | COUNT(*) | +------+----------+ | NULL | 1 | | f | 4 | | m | 4 | +------+----------+(In this output,NULLindicates that the sex is unknown.)Number of animals per combination of species and sex:mysql>
SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;+---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | bird | NULL | 1 | | bird | f | 1 | | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | | hamster | f | 1 | | snake | m | 1 | +---------+------+----------+You need not retrieve an entire table when you useCOUNT(). For example, the previous query, when performed just on dogs and cats, looks like this:mysql>
SELECT species, sex, COUNT(*) FROM pet->WHERE species = 'dog' OR species = 'cat'->GROUP BY species, sex;+---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | +---------+------+----------+Or, if you wanted the number of animals per sex only for animals whose sex is known:mysql>
SELECT species, sex, COUNT(*) FROM pet->WHERE sex IS NOT NULL->GROUP BY species, sex;+---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | bird | f | 1 | | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | | hamster | f | 1 | | snake | m | 1 | +---------+------+----------+If you name columns to select in addition to theCOUNT()value, aGROUP BYclause should be present that names those same columns. Otherwise, the following occurs:- If the
ONLY_FULL_GROUP_BYSQL mode is enabled, an error occurs:mysql>
SET sql_mode = 'ONLY_FULL_GROUP_BY';Query OK, 0 rows affected (0.00 sec) mysql>SELECT owner, COUNT(*) FROM pet;ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT()...) with no GROUP columns is illegal if there is no GROUP BY clause - If
ONLY_FULL_GROUP_BYis not enabled, the query is processed by treating all rows as a single group, but the value selected for each named column is indeterminate. The server is free to select the value from any row:mysql>
SET sql_mode = '';Query OK, 0 rows affected (0.00 sec) mysql>SELECT owner, COUNT(*) FROM pet;+--------+----------+ | owner | COUNT(*) | +--------+----------+ | Harold | 8 | +--------+----------+ 1 row in set (0.00 sec)
Using More Than one Table
Thepettable keeps track of which pets you have. If you want to record other information about them, such as events in their lives like visits to the vet or when litters are born, you need another table. What should this table look like? It needs to contain the following information:- The pet name so that you know which animal each event pertains to.
- A date so that you know when the event occurred.
- A field to describe the event.
- An event type field, if you want to be able to categorize events.
Given these considerations, theCREATE TABLEstatement for theeventtable might look like this:mysql>
CREATE TABLE event (name VARCHAR(20), date DATE,->type VARCHAR(15), remark VARCHAR(255));As with thepettable, it is easiest to load the initial records by creating a tab-delimited text file containing the following information.name date type remark Fluffy 1995-05-15 litter 4 kittens, 3 female, 1 male Buffy 1993-06-23 litter 5 puppies, 2 female, 3 male Buffy 1994-06-19 litter 3 puppies, 3 female Chirpy 1999-03-21 vet needed beak straightened Slim 1997-08-03 vet broken rib Bowser 1991-10-12 kennel Fang 1991-10-12 kennel Fang 1998-08-28 birthday Gave him a new chew toy Claws 1998-03-17 birthday Gave him a new flea collar Whistler 1998-12-09 birthday First birthday Load the records like this:mysql>
LOAD DATA LOCAL INFILE 'event.txt' INTO TABLE event;Based on what you have learned from the queries that you have run on thepettable, you should be able to perform retrievals on the records in theeventtable; the principles are the same. But when is theeventtable by itself insufficient to answer questions you might ask?Suppose that you want to find out the ages at which each pet had its litters. We saw earlier how to calculate ages from two dates. The litter date of the mother is in theeventtable, but to calculate her age on that date you need her birth date, which is stored in thepettable. This means the query requires both tables:mysql>
SELECT pet.name,->(YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age,->remark->FROM pet INNER JOIN event->ON pet.name = event.name->WHERE event.type = 'litter';+--------+------+-----------------------------+ | name | age | remark | +--------+------+-----------------------------+ | Fluffy | 2 | 4 kittens, 3 female, 1 male | | Buffy | 4 | 5 puppies, 2 female, 3 male | | Buffy | 5 | 3 puppies, 3 female | +--------+------+-----------------------------+There are several things to note about this query:- The
FROMclause joins two tables because the query needs to pull information from both of them. - When combining (joining) information from multiple tables, you need to specify how records in one table can be matched to records in the other. This is easy because they both have a
namecolumn. The query uses anONclause to match up records in the two tables based on thenamevalues.The query uses anINNER JOINto combine the tables. AnINNER JOINpermits rows from either table to appear in the result if and only if both tables meet the conditions specified in theONclause. In this example, theONclause specifies that thenamecolumn in thepettable must match thenamecolumn in theeventtable. If a name appears in one table but not the other, the row will not appear in the result because the condition in theONclause fails. - Because the
namecolumn occurs in both tables, you must be specific about which table you mean when referring to the column. This is done by prepending the table name to the column name.
You need not have two different tables to perform a join. Sometimes it is useful to join a table to itself, if you want to compare records in a table to other records in that same table. For example, to find breeding pairs among your pets, you can join thepettable with itself to produce candidate pairs of males and females of like species:mysql>
SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species->FROM pet AS p1 INNER JOIN pet AS p2->ON p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';+--------+------+--------+------+---------+ | name | sex | name | sex | species | +--------+------+--------+------+---------+ | Fluffy | f | Claws | m | cat | | Buffy | f | Fang | m | dog | | Buffy | f | Bowser | m | dog | +--------+------+--------+------+---------+In this query, we specify aliases for the table name to refer to the columns and keep straight which instance of the table each column reference is associated with.
No comments:
Post a Comment
Please feel free to contact or comment the article