⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manual_tutorial.html

📁 关于mysql学习的 中文教程
💻 HTML
📖 第 1 页 / 共 5 页
字号:

<p>SQL的模式匹配允许你使用<samp>“_”</samp>匹配任何单个字符,而<samp>“%”</samp>匹配任意数目字符(包括零个字符)。在 
MySQL中,SQL的模式缺省是忽略大小写的。下面显示一些例子。注意在你使用SQL模式时,你不能使用<code>=</code>或<code>!=</code>;而使用<code>LIKE</code>或<code>NOT 
LIKE</code>比较操作符。 </p>

<p>为了找出以<samp>“b”</samp>开头的名字: </p>

<pre>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;b%&quot;;
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
</pre>

<p>为了找出以<samp>“fy”</samp>结尾的名字: </p>

<pre>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;%fy&quot;;
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+
</pre>

<p>为了找出包含一个<samp>“w”</samp>的名字: </p>

<pre>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;%w%&quot;;
+----------+-------+---------+------+------------+------------+
| 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       |
+----------+-------+---------+------+------------+------------+
</pre>

<p>为了找出包含正好5个字符的名字,使用<samp>“_”</samp>模式字符: 
</p>

<pre>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;_____&quot;;
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
</pre>

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

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

<ul>
  <li><samp>“.”</samp>匹配任何单个的字符。 </li>
  <li>一个字符类<samp>“[...]”</samp>匹配在方括号内的任何字符。例如,<samp>“[abc]”</samp>匹配<samp>“a”</samp>、<samp>“b”</samp>或<samp>“c”</samp>。为了命名字符的一个范围,使用一个“-”。<samp>“[a-z]”</samp>匹配任何小写字母,而<samp>“[0-9]”</samp>匹配任何数字。 
  </li>
  <li><samp>“ * ”</samp>匹配零个或多个在它前面的东西。例如,<samp>“x*”</samp>匹配任何数量的<samp>“x”</samp>字符,<samp>“[0-9]*”</samp>匹配的任何数量的数字,而<samp>“.*”</samp>匹配任何数量的任何东西。 
  </li>
  <li>正则表达式是区分大小写的,但是如果你希望,你能使用一个字符类匹配两种写法。例如,<samp>“[aA]”</samp>匹配小写或大写<samp>的“a”</samp>而<samp>“[a-zA-Z]”</samp>匹配两种写法的任何字母。 
  </li>
  <li>如果它出现在被测试值的任何地方,模式就匹配(只要他们匹配整个值,SQL模式匹配)。 
  </li>
  <li>为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用<samp>“^”</samp>或<samp>在模式的结尾用“$”</samp>。 
  </li>
</ul>

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

<p>为了找出以<samp>“b”</samp>开头的名字,使用<samp>“^”</samp>匹配名字的开始并且<samp>“[bB]”</samp>匹配小写或大写<samp>的“b”</samp>: 
</p>

<pre>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;^[bB]&quot;;
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
</pre>

<p>为了找出以<samp>“fy”</samp>结尾的名字,使用<samp>“$”</samp>匹配名字的结尾: 
</p>

<pre>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;fy$&quot;;
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+
</pre>

<p>为了找出包含一个<samp>“w”</samp>的名字,使用<samp>“[wW]”</samp>匹配小写或大写<samp>的“w”</samp>: 
</p>

<pre>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;[wW]&quot;;
+----------+-------+---------+------+------------+------------+
| 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       |
+----------+-------+---------+------+------------+------------+
</pre>

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

<p>为了找出包含正好5个字符的名字,使用<samp>“^”</samp>和<samp>“$”</samp>匹配名字的开始和结尾,和5个<samp>“.”</samp>实例在两者之间: 
</p>

<pre>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;^.....$&quot;;
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
</pre>

<p>你也可以使用<samp>“{n}”</samp>“重复<code>n</code>次”操作符重写先前的查询: 
</p>

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

<h4><a NAME="Counting_rows" HREF="manual_toc.html#Counting_rows">8.4.4.8 行计数</a></h4>

<p>数据库经常用于回答这个问题,“某个类型的数据在一张表中出现的频度?”例如,你可能想要知道你有多少宠物,或每位主人有多少宠物,或你可能想要在你的动物上施行各种类型的普查。 
</p>

<p>计算你拥有动物的总数字与“在<code>pet</code>表中有多少行?”是同样的问题,因为每个宠物有一个记录。<code>COUNT()</code>函数计数非<code>NULL</code>结果的数目,所以数你的动物的查询看起来像这样: 
</p>

<pre>mysql&gt; SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
|        9 |
+----------+
</pre>

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

<pre>mysql&gt; SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  |        2 |
| Diane  |        2 |
| Gwen   |        3 |
| Harold |        2 |
+--------+----------+
</pre>

<p>注意,使用<code>GROUP BY</code>对每个<code>owner</code>分组所有记录,没有它,你得到的一切是一条错误消息: 
</p>

<pre>mysql&gt; SELECT owner, COUNT(owner) FROM pet;
ERROR 1140 at line 1: Mixing of GROUP columns (MIN(),MAX(),COUNT()...)
with no GROUP columns is illegal if there is no GROUP BY clause
</pre>

<p><code>COUNT()</code>和<code>GROUP BY</code>对以各种方式分类你的数据很有用。下列例子显示出实施动物普查操作的不同方式。 
</p>

<p>每种动物数量: </p>

<pre>mysql&gt; SELECT species, COUNT(*) FROM pet GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| bird    |        2 |
| cat     |        2 |
| dog     |        3 |
| hamster |        1 |
| snake   |        1 |
+---------+----------+
</pre>

<p>每中性别的动物数量: </p>

<pre>mysql&gt; SELECT sex, COUNT(*) FROM pet GROUP BY sex;
+------+----------+
| sex  | COUNT(*) |
+------+----------+
| NULL |        1 |
| f    |        4 |
| m    |        4 |
+------+----------+
</pre>

<p>(在这个输出中,<code>NULL</code>表示“未知性别”。)</p>

<p>按种类和性别组合的动物数量: </p>

<pre>mysql&gt; 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 |
+---------+------+----------+
</pre>

<p>当你使用<code>COUNT()</code>时,你不必检索整个一张表。例如, 
先前的查询,当只在狗和猫上施行时,看起来像这样: </p>

<pre>mysql&gt; SELECT species, sex, COUNT(*) FROM pet
    -&gt; WHERE species = &quot;dog&quot; OR species = &quot;cat&quot;
    -&gt; GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
+---------+------+----------+
</pre>

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

<pre>mysql&gt; SELECT species, sex, COUNT(*) FROM pet
    -&gt; WHERE sex IS NOT NULL
    -&gt; 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 |
+---------+------+----------+
</pre>

<h3><a NAME="Multiple_tables" HREF="manual_toc.html#Multiple_tables">8.4.5 
使用多个数据库表</a></h3>

<p><code>pet</code>表追踪你有哪个宠物。如果你想要记录他们的其他信息,例如在他们一生中事件看兽医或何时后代出生,你需要另外的表。这张表应该像什么呢? 

<ul>
  <li>它需要包含宠物名字因此你知道每个事件属于此动物。 </li>
  <li>它需要一个日期因此你知道事件什么时候发生的。 </li>
  <li>需要一个字段描述事件。 </li>
  <li>如果你想要可分类事件,有一个事件类型字段将是有用的。 </li>
</ul>

<p>给出了这些考虑,为<code>event</code>表的<code>CREATE TABLE</code>语句可能看起来像这样: 
</p>

<pre>mysql&gt; CREATE TABLE event (name VARCHAR(20), date DATE,
    -&gt; type VARCHAR(15), remark VARCHAR(255));
</pre>

<p>就象<code>pet</code>表,最容易的示通过创建包含信息的一个定位符分隔的文本文件装载初始记录: 
</p>

<table border="1" width="100%" NOSAVE class="p4">
<tbody>
  <tr>
    <td height="16">Fluffy </td>
    <td height="16">1995-05-15 </td>
    <td height="16">litter </td>
    <td height="16">4 kittens, 3 female, 1 male </td>
  </tr>
  <tr>
    <td height="16">Buffy </td>
    <td height="16">1993-06-23 </td>
    <td height="16">litter </td>
    <td height="16">5 puppies, 2 female, 3 male </td>
  </tr>
  <tr>
    <td height="16">Buffy </td>
    <td height="16">1994-06-19 </td>
    <td height="16">litter </td>
    <td height="16">3 puppies, 3 female </td>
  </tr>
  <tr>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -