Home > Database > Mysql Tutorial > body text

MySQL Getting Started Tutorial 5 - Retrieving Information from Data Tables

黄舟
Release: 2017-02-23 11:39:40
Original
1245 people have browsed it



#The SELECT statement is used to retrieve information from a data table. The general format of the statement is:

SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;
Copy after login

what_to_select points out what you want to see, which can be a table of columns, or * means "all columns". which_table indicates the table from which you want to retrieve data. The WHERE clause is optional. If this item is selected, conditions_to_satisfy specifies the retrieval conditions that the row must meet.

1. Select all data

The simplest form of SELECT is to retrieve all records 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       |
+----------+--------+---------+------+------------+------------+
Copy after login

If you want to browse the entire table, you can use this form of SELECT, for example, just after loading the initial data set. Or maybe you're thinking that Bowser's birthday doesn't look right. Checking your original family tree, you find that the correct year of birth is 1989, not 1979.

There are at least two ways to correct it:

·Edit the file "pet.txt" to correct the error, and then use DELETE and LOAD DATA to clear and reload the table:

 mysql> DELETE FROM pet; mysql> LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;
Copy after login

However , If you do this, you must re-enter the Puffball record.

·Use an UPDATE statement to correct only the wrong records:

mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';
Copy after login

UPDATE only changes the problematic records and does not require reloading the database table.

2. Select special rows

As shown above, retrieving the entire table is easy. Just remove the WHERE clause from the SELECT statement. But generally you don't want to see the entire table, especially when the table becomes very large. Instead, you are usually more interested in answering a specific question, in which case placing some restrictions on the information you want. Let’s look at some of the select queries they answer for questions about your pet. You can select only specific rows from the table. For example, if you want to verify the change you made to Bowser's birthday, select Bowser's record as follows:

mysql> SELECT * FROM pet WHERE name = 'Bowser'; +--------+-------+---------+------+------------+------------+
| name   | owner | species | sex  | birth      | death      |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
Copy after login

The output confirms that the correct year record is 1989, not 1979.

String comparison is usually insensitive to size, so you can specify the name as "bowser", "BOWSER", etc., and the query results will be the same.

You can specify conditions on any column, not just name. For example, if you want to know which animals were born after 1998, test the birth column:

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  |
+----------+-------+---------+------+------------+-------+
Copy after login

You can combine conditions, for example, to find female dogs:

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
Copy after login
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
Copy after login

The above query uses AND Logical operators, there is also an OR operator:

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  |
+----------+-------+---------+------+------------+-------+
Copy after login

AND and OR can be mixed, but AND has a higher priority than OR. If you are using two operators, it is a good idea to use parentheses to indicate how to group the conditions:

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  |
+-------+--------+---------+------+------------+-------+
Copy after login

3. Select special columns

If you don’t want to see all rows in the table , just name the columns you are interested in, separated by commas. For example, if you want to know when your animal was born, select the name and birth columns:

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 |
+----------+------------+
Copy after login

To find out who owns the pet, use this query:

mysql> SELECT owner FROM pet; +--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| Harold |
| Benny  |
| Diane  |
| Gwen   |
| Gwen   |
| Benny  |
| Diane  |
+--------+
Copy after login

Please note that this query is just Simply retrieve the owner column of each record and some of them appear multiple times. To minimize output, add the keyword DISTINCT to retrieve each unique output record:

mysql> SELECT DISTINCT owner FROM pet; +--------+
| owner  |
+--------+
| Benny  |
| Diane  |
| Gwen   |
| Harold |
+--------+
Copy after login

You can use a WHERE clause to combine row selection and column selection. For example, to query the birth dates of dogs and cats, 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 |
+--------+---------+------------+
Copy after login

4. Category rows

You may have noticed that the result rows in the previous example are not displayed in a specific order. . However, it's often easier to examine query output when the rows are sorted in a certain way. To sort the results, use the ORDER BY clause. Here are the 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 |
+----------+------------+
Copy after login

On character type columns, like all other comparison operations, the sort function is normally performed in a case-sensitive manner. This means that the order of identical columns with different case is not defined. For a certain column, you can use BINARY to enforce case-sensitive classification, such as: ORDER BY BINARY col_name.

The default sorting is ascending order, with the smallest value first. To sort in descending order, add the DESC (descending order) keyword to the column name you are sorting:

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 |
+----------+------------+
Copy after login

You can sort multiple columns, and you can sort different columns in different directions. For example, to sort animal species in ascending order, and then sort each animal species by birth date in descending order (youngest animal 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 |
+----------+---------+------------+
Copy after login

Note that the DESC keyword only applies to The column name before it (birth); does not affect the sort order of the species column.

5. Date calculation

MySQL provides several functions that can be used to calculate dates, such as calculating age or extracting date parts.

To determine how old each pet is, calculate the difference between the year of the current date and the date of birth. If the calendar year of the current date is earlier than the date of birth, one year is subtracted. The following query displays the year number for each pet's birth date, current date, and age value.

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 |
+----------+------------+------------+------+
Copy after login

Here, YEAR() extracts the year part of the date, and RIGHT() extracts the rightmost 5 characters of the MM-DD (calendar year) part of the date. The value of the expression part comparing the MM-DD value is generally 1 or 0. If the year of CURDATE() is earlier than the year of birth, the year should be subtracted by 1. The whole expression is a bit confusing, use alias (age) to make the output column labels more meaningful.

尽管查询可行,如果以某个顺序排列行,则能更容易地浏览结果。添加ORDER BY name子句按照名字对输出进行排序则能够实现。

mysql> SELECT name, birth, CURDATE(),    -> (YEAR(CURDATE())-YEAR(birth))    -> - (RIGHT(CURDATE(),5)<RIGHT(birth,5))    -> AS age
Copy after login

-> 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 |
+----------+------------+------------+------+
Copy after login

为了按age而非name排序输出,只要再使用一个ORDER BY子句:

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 |
+----------+------------+------------+------+
Copy after login

可以使用一个类似的查询来确定已经死亡动物的死亡年龄。你通过检查death值是否是NULL来确定是哪些动物,然后,对于那些非NULL值的动物,需要计算出death和birth值之间的差:

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 |
+--------+------------+------------+------+
Copy after login

查询使用death IS NOT NULL而非death != NULL,因为NULL是特殊的值,不能使用普通比较符来比较,以后会给出解释。

如果你想要知道哪个动物下个月过生日,怎么办?对于这类计算,年和天是无关的,你只需要提取birth列的月份部分。MySQL提供几个日期部分的提取函数,例如YEAR( )、MONTH( )和DAYOFMONTH( )。在这里MONTH()是适合的函数。为了看它怎样工作,运行一个简单的查询,显示birth和MONTH(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 |
+----------+------------+--------------+
Copy after login

找出下个月生日的动物也是容易的。假定当前月是4月,那么月值是4,你可以找在5月出生的动物 (5月),方法是:

mysql> SELECT name, birth FROM pet WHERE MONTH(birth) = 5; +-------+------------+
| name  | birth      |
+-------+------------+
| Buffy | 1989-05-13 |
+-------+------------+
Copy after login

如果当前月份是12月,就有点复杂了。你不能只把1加到月份数(12)上并寻找在13月出生的动物,因为没有这样的月份。相反,你应寻找在1月出生的动物(1月) 。

你甚至可以编写查询,不管当前月份是什么它都能工作。采用这种方法不必在查询中使用一个特定的月份,DATE_ADD( )允许在一个给定的日期上加上时间间隔。如果在NOW( )值上加上一个月,然后用MONTH()提取月份,结果产生生日所在月份:

mysql> SELECT name, birth FROM pet     -> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
Copy after login

完成该任务的另一个方法是加1以得出当前月份的下一个月(在使用取模函数(MOD)后,如果月份当前值是12,则“回滚”到值0):

mysql> SELECT name, birth FROM pet     -> WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
Copy after login

注意,MONTH返回在1和12之间的一个数字,且MOD(something,12)返回在0和11之间的一个数字,因此必须在MOD( )以后加1,否则我们将从11月( 11 )跳到1月(1)。

6. NULL值操作

NULL值可能令人感到奇怪直到你习惯它。概念上,NULL意味着“没有值”或“未知值”,且它被看作与众不同的值。为了测试NULL,你不能使用算术比较 操作符例如=、<或!=。为了说明它,试试下列查询:

mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL; +----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
|     NULL |      NULL |     NULL |     NULL |
+----------+-----------+----------+----------+
Copy after login

很显然你不能通过这些比较得到有意义的结果。相反使用IS NULL和IS NOT NULL操作符:

mysql> SELECT 1 IS NULL, 1 IS NOT NULL; +-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
|         0 |             1 |
+-----------+---------------+
Copy after login

请注意在MySQL中,0或 NULL意味着假而其它值意味着真。布尔运算的默认真值是1。

对NULL的特殊处理即是在前面的章节中,为了决定哪个动物不再是活着的,使用death IS NOT NULL而不使用death != NULL的原因。

在GROUP BY中,两个NULL值视为相同。

执行ORDER BY时,如果运行 ORDER BY ... ASC,则NULL值出现在最前面,若运行ORDER BY ... DESC,则NULL值出现在最后面。

NULL操作的常见错误是不能在定义为NOT NULL的列内插入0或空字符串,但事实并非如此。在NULL表示"没有数值"的地方有数值。使用IS [NOT] NULL则可以很容易地进行测试,如下所示:

mysql> SELECT 0 IS NULL, 0 IS NOT NULL, &#39;&#39; IS NULL, &#39;&#39; IS NOT NULL; +-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | &#39;&#39; IS NULL | &#39;&#39; IS NOT NULL |
+-----------+---------------+------------+----------------+
|         0 |             1 |          0 |              1 |
+-----------+---------------+------------+----------------+
Copy after login

因此完全可以在定义为NOT NULL的列内插入0或空字符串,实际是NOT NULL。

7. 模式匹配

MySQL提供标准的SQL模式匹配,以及一种基于象Unix实用程序如vigrepsed的扩展正则表达式模式匹配的格式。

SQL模式匹配允许你使用

“_”

匹配任何单个字符,而

“%”

匹配任意数目字符(包括零字符)。在 MySQL中,SQL的模式默认是忽略大小写的。下面给出一些例子。注意使用SQL模式时,不能使用=或!=;而应使用LIKE或NOT LIKE比较操作符。

要想找出以

“b”

开头的名字:

mysql> SELECT * FROM pet WHERE name LIKE &#39;b%&#39;; +--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
Copy after login

要想找出以

“fy”

结尾的名字:

mysql> SELECT * FROM pet WHERE name LIKE &#39;%fy&#39;; +--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+
Copy after login

要想找出包含

“w”

的名字:

mysql> SELECT * FROM pet WHERE name LIKE &#39;%w%&#39;; +----------+-------+---------+------+------------+------------+
| 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       |
+----------+-------+---------+------+------------+------------+
Copy after login

要想找出正好包含5个字符的名字,使用

“_”

模式字符:

mysql> SELECT * FROM pet WHERE name LIKE &#39;_____&#39;; +-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
Copy after login

MySQL提供的模式匹配的其它类型是使用扩展正则表达式。当你对这类模式进行匹配测试时,使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它们是同义词)。

扩展正则表达式的一些字符是:

· ‘.’匹配任何单个的字符。

· 字符类

“[...]”

匹配在方括号内的任何字符。例如,

“[abc]”

匹配

“a”

“b”

“c”

。为了命名字符的范围,使用一个“-”。

“[a-z]”

匹配任何字母,而

“[0-9]”

匹配任何数字。

·

“ * ”

匹配零个或多个在它前面的字符。例如,

“x*”

匹配任何数量的

“x”

字符,

“[0-9]*”

匹配任何数量的数字,而

“.*”

匹配任何数量的任何字符。

  • 如果REGEXP模式与被测试值的任何地方匹配,模式就匹配(这不同于LIKE模式匹配,只有与整个值匹配,模式才匹配)。

  • 为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用
    “^”

    在模式的结尾用“$”

为了说明扩展正则表达式如何工作,下面使用REGEXP重写上面所示的LIKE查询:

为了找出以

“b”

开头的名字,使用

“^”

匹配名字的开始:

mysql> SELECT * FROM pet WHERE name REGEXP &#39;^b&#39;; +--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
Copy after login

如果你想强制使REGEXP比较区分大小写,使用BINARY关键字使其中一个字符串变为二进制字符串。该查询只匹配名称首字母的小写‘b’。

mysql> SELECT * FROM pet WHERE name REGEXP BINARY &#39;^b&#39;;
Copy after login

为了找出以

“fy”

结尾的名字,使用

“$”

匹配名字的结尾:

mysql> SELECT * FROM pet WHERE name REGEXP &#39;fy$&#39;; +--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+
Copy after login

为了找出包含一个

“w”

的名字,使用以下查询:

mysql> SELECT * FROM pet WHERE name REGEXP &#39;w&#39;; +----------+-------+---------+------+------------+------------+
| 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       |
+----------+-------+---------+------+------------+------------+
Copy after login

既然如果一个正则表达式出现在值的任何地方,其模式匹配了,就不必在先前的查询中在模式的两侧放置一个通配符以使得它匹配整个值,就像你使用了一个SQL模式那样。

为了找出包含正好5个字符的名字,使用

“^”

“$”

匹配名字的开始和结尾,和5个

“.”

实例在两者之间:

mysql> SELECT * FROM pet WHERE name REGEXP &#39;^.....$&#39;; +-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
Copy after login

你也可以使用

“{n}”

“重复n次”操作符重写前面的查询:

mysql> SELECT * FROM pet WHERE name REGEXP &#39;^.{5}$&#39;; +-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
Copy after login

8. 计数行

数据库经常用于回答这个问题,“某个类型的数据在表中出现的频度?”例如,你可能想要知道你有多少宠物,或每位主人有多少宠物,或你可能想要对你的动物进行各种类型的普查。计算你拥有动物的总数目与“在pet表中有多少行?”是同样的问题,因为每个宠物有一个记录。COUNT(*)函数计算行数,所以计算动物数目的查询应为:

mysql> SELECT COUNT(*) FROM pet; +----------+
| COUNT(*) |
+----------+
|        9 |
+----------+
Copy after login

在前面,你检索了拥有宠物的人的名字。如果你想要知道每个主人有多少宠物,你可以使用COUNT( )函数:

mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner; +--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  |        2 |
| Diane  |        2 |
| Gwen   |        3 |
| Harold |        2 |
+--------+----------+
Copy after login

注意,使用GROUP BY对每个owner的所有记录分组,没有它,你会得到错误消息:

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
Copy after login

COUNT( )和GROUP BY以各种方式分类你的数据。下列例子显示出进行动物普查操作的不同方式。

每种动物的数量:

mysql> SELECT species, COUNT(*) FROM pet GROUP BY species; +---------+----------+
| species | COUNT(*) |
+---------+----------+
| bird    |        2 |
| cat     |        2 |
| dog     |        3 |
| hamster |        1 |
| snake   |        1 |
+---------+----------+
Copy after login

每种性别的动物数量:

mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex; +------+----------+
| sex  | COUNT(*) |
+------+----------+
| NULL |        1 |
| f    |        4 |
| m    |        4 |
+------+----------+
Copy after login

(在这个输出中,NULL表示“未知性别”。)

按种类和性别组合的动物数量:

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 |
+---------+------+----------+
Copy after login

若使用COUNT( ),你不必检索整个表。例如, 前面的查询,当只对狗和猫进行时,应为:

mysql> SELECT species, sex, COUNT(*) FROM pet     -> WHERE species = &#39;dog&#39; OR species = &#39;cat&#39;     -> GROUP BY species, sex; +---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
+---------+------+----------+
Copy after login

或,如果你仅需要知道已知性别的按性别的动物数目:

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 |
+---------+------+----------+
Copy after login

9. 使用1个以上的表

pet表追踪你有哪个宠物。如果你想要记录其它相关信息,例如在他们一生中看兽医或何时后代出生,你需要另外的表。这张表应该像什么呢?需要:· 它需要包含宠物名字以便你知道每个事件属于哪个动物。

· 需要一个日期以便你知道事件是什么时候发生的。

· 需要一个描述事件的字段。

· 如果你想要对事件进行分类,则需要一个事件类型字段。

综合上述因素,event表的CREATE TABLE语句应为:

mysql> CREATE TABLE event (name VARCHAR(20), date DATE,     -> type VARCHAR(15), remark VARCHAR(255));
Copy after login

对于pet表,最容易的方法是创建包含信息的用定位符分隔的文本文件来装载初始记录:

namedatetyperemark
Fluffy1995-05-15litter4 kittens, 3 female, 1 male
Buffy1993-06-23litter5 puppies, 2 female, 3 male
Buffy1994-06-19litter3 puppies, 3 female
Chirpy1999-03-21vetneeded beak straightened
Slim1997-08-03vetbroken rib
Bowser1991-10-12kennel
Fang1991-10-12kennel
Fang1998-08-28birthdayGave him a new chew toy
Claws1998-03-17birthdayGave him a new flea collar
Whistler1998-12-09birthdayFirst birthday

采用如下方式装载记录:

mysql> LOAD DATA LOCAL INFILE &#39;event.txt&#39; INTO TABLE event;
Copy after login

根据你从已经运行在pet表上的查询中学到的,你应该能执行对event表中记录的检索;原理是一样的。但是什么时候event表本身不能回答你可能问的问题呢?

当他们有了一窝小动物时,假定你想要找出每只宠物的年龄。我们前面看到了如何通过两个日期计算年龄。event表中有母亲的生产日期,但是为了计算母亲的年龄,你需要她的出生日期,存储在pet表中。说明查询需要两个表:

mysql> SELECT pet.name,     -> (YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age,     -> remark     -> FROM pet, event     -> WHERE pet.name = event.name AND event.type = &#39;litter&#39;; +--------+------+-----------------------------+
| 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         |
+--------+------+-----------------------------+
Copy after login

关于该查询要注意的几件事情:

  • FROM子句列出两个表,因为查询需要从两个表提取信息。

  • 当从多个表组合(联结)信息时,你需要指定一个表中的记录怎样能匹配其它表的记录。这很简单,因为它们都有一个name列。查询使用WHERE子句基于name值来匹配2个表中的记录。

  • 因为name列出现在两个表中,当引用列时,你一定要指定哪个表。把表名附在列名前即可以实现。

你不必有2个不同的表来进行联结。如果你想要将一个表的记录与同一个表的其它记录进行比较,可以将一个表联结到自身。例如,为了在你的宠物之中繁殖配偶,你可以用pet联结自身来进行相似种类的雄雌配对:

mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species     -> FROM pet AS p1, pet AS p2     -> WHERE p1.species = p2.species AND p1.sex = &#39;f&#39; AND p2.sex = &#39;m&#39;; +--------+------+--------+------+---------+
| name   | sex  | name   | sex  | species |
+--------+------+--------+------+---------+
| Fluffy | f    | Claws  | m    | cat     |
| Buffy  | f    | Fang   | m    | dog     |
| Buffy  | f    | Bowser | m    | dog     |
+--------+------+--------+------+---------+
Copy after login

在这个查询中,我们为表名指定别名以便能引用列并且使得每一个列引用与哪个表实例相关联更直观。

 以上就是MySQL入门教程5 —— 从数据表中检索信息的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!