接上文
7、模式匹配
Mysql提供标准SQL模式匹配,"_"匹配任何单个字符,"%"匹配任意数目的字符(包括零字符)。在mysql中,sql的默认模式是忽略大小写的。注意在使用sql模式时,不能使用=或者!=,而应使用LIKE或NOT LIKE比较操作符。
如要想找出"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 |
+--------+--------+---------+------+------------+------------+
以"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 |
+--------+--------+---------+------+------------+-------+
或包含"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 |
+----------+-------+---------+------+------------+------------+
要想找到包含5个字符的名字,则用如下语句
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 |
+-------+--------+---------+------+------------+-------+
扩展正则表达式的一些字符是:
'."匹配任何单个字符
字符类"[....]"匹配在方括号内的任意字符。例如,"[abc]"匹配"a","b","c"。为了命名字符的范围,使用"-"。如"[a-z]"匹配任何字母。
"*"匹配0个或多个在它前面的字符。例如"x*"匹配任何数量的"x"字符, "[0-9]*"匹配任何数量的数字,而".*"匹配任何数量的任何字符。
为了定位一个模式,以便它必须匹配被测试值的开始或者结尾,在模式开始处使用"^", 或在模式结尾用"$"。
当使用以上所示的扩展模式匹配时,使用REGEXP或者NOT REGEXP操作符来代替LIKE, NOT LIKE。
如为了找出以"b"开头的名字,使用"^b"
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 |
+--------+--------+---------+------+------------+------------+
如果想要强制区分大小写,则使用BINARY
mysql> SELECT * FROM pet WHERE name REGEXP BINARY '^b';
为了找出以"fy"结尾的名字,使用"$"匹配名字的结尾:
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 |
+--------+--------+---------+------+------------+-------+
为了找出包含"w"的字符,使用以下查询
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 |
+----------+-------+---------+------+------------+------------+
可以使用“{n}”“重复n次”操作符重写前面的查询:
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 |
+-------+--------+---------+------+------------+-------+
8、计数行
COUNT(*)函数计算行数,所以计算动物的查询应为:
mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 9 |
+----------+
在前面,你检索了拥有宠物的人的名字。如果你想要知道每个主人有多少宠物,你可以使用COUNT( )函数
mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner | COUNT(*) |
+--------+----------+
| Benny | 2 |
| Diane | 2 |
| Gwen | 3 |
| Harold | 2 |
+--------+----------+
注意如果没有GROUP BY会得到错误消息。
按种类和性别组合的动物数量:
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 |
+---------+------+----------+
9、使用1个以上的表
在FROM子句中添加多个表格,并且用“,”分割,可以对多个表格进行联合查询
五、获取数据库和表格的信息
SHOW DATABASES可以显示当前所以的数据库。为了找出当前选择了那个数据库,使用DATABASE()函数:
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| menagerie |
+------------+
如果还没有选择任何数据库,则结果为NULL。
为了找出当前数据库包含什么表,使用这个命令:
mysql> SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| event |
| pet |
+---------------------+
如果想知道表的结构,使用命令DESCRIBE:
mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
其中Field为字段名称,Type为字段类型,Null表示该字段是否允许为NULL,Default为默认值是多少。
七、常用的查询例子
1、列的最大值
SELECT MAX(article) AS article FROM shop;
+---------+
| article |
+---------+
| 4 |
+---------+
2、拥有某个列的最大值的行
SELECT article, dealer, price
FROM shop
WHERE price=(SELECT MAX(price) FROM shop);
另外可以用降序排列,然后LIMIT子句只得到一行
SELECT article, dealer, price
FROM shop
ORDER BY price DESC
LIMIT 1;
但此方法中如果有多个商品最贵时LIMIT只能显示其中的一个。
3、按组求列的最大值
SELECT article, MAX(price) AS price
FROM shop
GROUP BY article
+---------+-------+
| article | price |
+---------+-------+
| 0001 | 3.99 |
| 0002 | 10.99 |
| 0003 | 1.69 |
| 0004 | 19.95 |
+---------+-------+