码迷,mamicode.com
首页 > 数据库 > 详细

Mysql数据库(五)表记录的检索

时间:2018-04-17 19:59:12      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:不同   记录   字符集   asc   模糊查询   sts   sql   长度   多个   

  一、基本查询语句

  二、单表查询

  1.查询所有字段

mysql> SELECT * FROM tb_bookinfo;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)

   2.查询指定字段

mysql> SELECT bookname,author FROM tb_bookinfo;
+-----------+-----------+
| bookname  | author    |
+-----------+-----------+
| Java King | LianJiang |
| Lian      | QiaoJiang |
| Tian King | TianJiang |
+-----------+-----------+
3 rows in set (0.00 sec)

   3.查询指定数据

mysql> SELECT * FROM tb_bookinfo WHERE bookname=‘Tian King‘;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
1 row in set (0.00 sec)

   4.带IN关键字的查询

mysql> SELECT bookname,author,price,page,bookcase FROM tb_bookinfo WHERE bookcase IN(1,2);
+-----------+-----------+-------+------+----------+
| bookname  | author    | price | page | bookcase |
+-----------+-----------+-------+------+----------+
| Java King | LianJiang | 49.80 |  350 |        1 |
| Lian      | QiaoJiang | 50.00 |  351 |        2 |
+-----------+-----------+-------+------+----------+
2 rows in set (0.00 sec)

mysql> SELECT bookname,author,price,page,bookcase FROM tb_bookinfo WHERE bookcase IN(1,3);
+-----------+-----------+-------+------+----------+
| bookname  | author    | price | page | bookcase |
+-----------+-----------+-------+------+----------+
| Java King | LianJiang | 49.80 |  350 |        1 |
| Tian King | TianJiang | 51.10 |  352 |        3 |
+-----------+-----------+-------+------+----------+
2 rows in set (0.00 sec)

   5.带BETWEEN AND的范围查询

mysql> SELECT * FROM tb_bookinfo WHERE inTime BETWEEN ‘2017-04-17‘ and ‘2017-04-19‘;
Empty set (0.00 sec)

mysql> SELECT * FROM tb_bookinfo WHERE inTime BETWEEN ‘2018-04-17‘ and ‘2018-04-19‘;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)

mysql> SELECT * FROM tb_bookinfo WHERE inTime BETWEEN ‘2018-04-17‘ and ‘2018-04-18‘;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
2 rows in set (0.00 sec)

mysql> SELECT * FROM tb_bookinfo WHERE inTime NOT BETWEEN ‘2018-04-17‘ and ‘2018-04-18‘;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
1 row in set (0.00 sec)

   6.带LIKE的字符匹配查询,通过它可以实现模糊查询,有两种通配符:%(可以匹配一个或多个字符,可以代表任意长度的字符串)和_(只匹配一个字符)

mysql> SELECT * FROM tb_bookinfo WHERE barcode LIKE ‘%71%‘;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)

  7.用IS NULL关键字查询空值

mysql> SELECT * FROM tb_bookinfo WHERE bookname IS NULL;
Empty set (0.00 sec)

   8.带AND的多条件查询

mysql> SELECT * FROM tb_bookinfo WHERE bookcase=1 AND del=0;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
1 row in set (0.00 sec)

   9.带OR的多条件查询

mysql> SELECT * FROM tb_bookinfo WHERE bookcase=1 OR bookcase=2;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
2 rows in set (0.00 sec)

   10.用DISTINCT关键字去除结果中的重复行

mysql> SELECT DISTINCT del FROM tb_bookinfo ;
+------+
| del  |
+------+
|    0 |
+------+
1 row in set (0.00 sec)

  11.用ORDER BY关键字对查询结果排序(ASC表示升序,DESC表示降序)

mysql> SELECT * FROM tb_bookinfo ORDER BY price DESC;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)
mysql> SELECT * FROM tb_bookinfo ORDER BY typeid ASC;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)

   12.用GROUP BY关键字分组查询

  (1)使用GROUP BY关键字分组查询

mysql> SELECT del,COUNT(*) FROM tb_bookinfo GROUP BY del;
+------+----------+
| del  | COUNT(*) |
+------+----------+
|    0 |        3 |
+------+----------+
1 row in set (0.00 sec)

  (2)GROUP BY关键字与GROUP_CONCAT()函数一起使用

mysql> SELECT del,GROUP_CONCAT(barcode) FROM tb_bookinfo GROUP BY del;
+------+----------------------------+
| del  | GROUP_CONCAT(barcode)      |
+------+----------------------------+
|    0 | 17120107,17120108,17120109 |
+------+----------------------------+
1 row in set (0.00 sec)

  (3)按多个字段进行分组,当第一个字段有相同值时,再按第二个字段进行分组

mysql> SELECT del,barcode FROM tb_bookinfo GROUP BY del,barcode;
+------+----------+
| del  | barcode  |
+------+----------+
|    0 | 17120107 |
|    0 | 17120108 |
|    0 | 17120109 |
+------+----------+
3 rows in set (0.00 sec)

  13.用LIMIT限制查询结果的数量(显示前两条数据和从第1个编号开始(记录编号是从0开始的),查询两条数据)

mysql> SELECT * FROM tb_bookinfo ORDER BY price DESC LIMIT 2;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
2 rows in set (0.00 sec)
mysql> SELECT * FROM tb_bookinfo ORDER BY price DESC LIMIT 1,2;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
2 rows in set (0.00 sec)

  三、聚合函数查询

  聚合函数的最大特点是它们根据一组数据求出一个值。聚合函数的结果值只根据选定行中非NULL的值进行计算,NULL值被忽略。

  1.COUNT()函数用于对除“*”以外的任何参数,返回所选择集合中非NULL值的行的数目;对于参数“*”,返回选择集合中所有行的数目,包含NULL值的行。

mysql> SELECT COUNT(*) FROM tb_bookinfo;
+----------+
| COUNT(*) |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

  2.SUM()函数

mysql> SELECT price FROM tb_bookinfo;
+-------+
| price |
+-------+
| 49.80 |
| 50.00 |
| 51.10 |
+-------+
3 rows in set (0.00 sec)

mysql> SELECT SUM(price) FROM tb_bookinfo;
+------------+
| SUM(price) |
+------------+
|     150.90 |
+------------+
1 row in set (0.00 sec)

  3.AVG()函数

mysql> SELECT AVG(price) FROM tb_bookinfo;
+------------+
| AVG(price) |
+------------+
|  50.299999 |
+------------+
1 row in set (0.00 sec)

  4.MAX()函数

mysql> SELECT MAX(price) FROM tb_bookinfo;
+------------+
| MAX(price) |
+------------+
|      51.10 |
+------------+
1 row in set (0.00 sec)

  5.MIN()函数

mysql> SELECT MIN(price) FROM tb_bookinfo;
+------------+
| MIN(price) |
+------------+
|      49.80 |
+------------+
1 row in set (0.00 sec)

  四、连接查询

  1.内连接查询

  连接是把不同表的记录连到一起的最普遍的方法。内连接是最普遍的连接类型,而且是最匀称的,因为它们要求构成连接的每个表的共有列匹配,不匹配的行将被排除。内连接包括相等连接和自然连接,最常见的例子是相等连接,也就是使用等号运算符根据每个表共有的列的值匹配两个表的行。这种情况下,最后的结果集只包含参加连接的表中与指定字段相符的行。

mysql> SELECT * FROM tb_bookinfo;
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode  | bookname  | typeid | author    | ISBN | price | page | bookcase | inTime     | del  | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King |      3 | LianJiang | 115  | 49.80 |  350 |        1 | 2018-04-17 |    0 |  1 |
| 17120108 | Lian      |      1 | QiaoJiang | 116  | 50.00 |  351 |        2 | 2018-04-18 |    0 |  2 |
| 17120109 | Tian King |      2 | TianJiang | 117  | 51.10 |  352 |        3 | 2018-04-19 |    0 |  3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)

mysql> SELECT * FROM tb_borrow;
+----+----------+--------+------------+------------+----------+--------+
| id | readerid | bookid | borrowTime | backTime   | operator | ifback |
+----+----------+--------+------------+------------+----------+--------+
|  1 |        1 |      1 | 2018-04-17 | 2018-04-20 | mr       |      1 |
|  2 |        1 |      2 | 2018-04-16 | 2018-04-21 | mr       |      1 |
+----+----------+--------+------------+------------+----------+--------+
2 rows in set (0.00 sec)

mysql> SELECT bookid,borrowTime,backTime,ifback,bookname,author,price
    -> FROM tb_borrow,tb_bookinfo WHERE tb_borrow.bookid=tb_bookinfo.id;
+--------+------------+------------+--------+-----------+-----------+-------+
| bookid | borrowTime | backTime   | ifback | bookname  | author    | price |
+--------+------------+------------+--------+-----------+-----------+-------+
|      1 | 2018-04-17 | 2018-04-20 |      1 | Java King | LianJiang | 49.80 |
|      2 | 2018-04-16 | 2018-04-21 |      1 | Lian      | QiaoJiang | 50.00 |
+--------+------------+------------+--------+-----------+-----------+-------+
2 rows in set (0.00 sec)

  2.外连接查询

  与内连接不同,外连接是指使用OUTET JOIN关键字将两个表连接起来。外连接生成的结果集不仅包含符合连接条件的行数据,而且含包括左表、右表或两边连接表中所有的数据行。

  3.复合条件连接查询

  五、子查询

  1.带IN关键字的子查询

  2.带比较运算符的子查询

  3.带EXISTS关键字的子查询

  4.带ANY关键字的子查询

  5.带ALL关键字的子查询

  六、合并查询结果

  1.使用UNION关键字

  2.使用UNION ALL关键字

  七、定义表和字段的别名

  1.为表取别名

  2.为字段取别名

  八、使用正则表达式查询

  1.匹配指定字符集中任意一个

  2.使用“*”和“+”来匹配多个字符

Mysql数据库(五)表记录的检索

标签:不同   记录   字符集   asc   模糊查询   sts   sql   长度   多个   

原文地址:https://www.cnblogs.com/BigJunOba/p/8868226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!