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

  一、基本查询语句

  二、单表查询

  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.使用“*”和“+”来匹配多个字符

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

时间: 2024-10-05 23:17:43

Mysql数据库(五)表记录的检索的相关文章

mysql数据库之表记录

对表记录的操作http://www.cnblogs.com/linhaifeng/articles/7267587.html增:insert 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3-字段n) VALUES(值1,值2,值3-值n); 语法二: INSERT INTO 表名 VALUES (值1,值2,值3-值n); 2. 指定字段插入数据 语法: INSERT INTO 表名(字段1,字段2,字段3-) VALUES (值1,值2,值3-)

MySQL数据库以及表的管理

MySQL数据库以及表的管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 今天我们探讨的话题就是如何使用MySQL做开发,我们运维的主要工作不是去开发SQL的,但尽管如此,我们有可能需要对别人写出来的复杂SQL语句做性能评估,去分析他们写的SQL语句是不是足够高效,如果不是的话,我们还要能够达到对对方的SQL语句做改写的能力.所以,我们无需自行去开发SQL,但是我们一定要有一个看懂别人写的SQL的能力哟! 一.DBA的职责 1.开发DBA: 负责数据库设计(E-R关系图).

MySQL数据库之表的增删改查

目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是硬盘上的一个文件夹,表table就是文件夹里面的一个文件 2.表中的一条记录就相当于文件中的一行内容,与excel表类似,表table中有不同的标题,称之为字段 3.本节对表结构做增删改查,即创建表.删除表.修改表.查看表结构,至于表中数据的增删改查日后单独总结 2 创建表 1.创建一张表,需要建一

sql语句备份/导入 mysql数据库或表命令

sql语句备份/导入 mysql数据库或表命令,布布扣,bubuko.com

MySQL数据库、表的字符编码

用MySQL命令行新建数据库和表时默认的字符编码是latin1,但是在实际开发过程中一般都是使用utf8格式的编码.操作如下: 1.修改数据库字符编码 mysql> alter database mydb character set utf8 ;2.创建数据库时,指定数据库的字符编码 mysql> create database mydb character set utf8 ;3.查看mysql数据库的字符编码 mysql> show variables like 'character

mysql数据库单表只有一个主键自增id字段,ibatis实现id自增

mysql数据库单表只有一个主键自增id字段,ibatis实现id自增 <insert id="autoid">        insert into user_id values(null)        <selectKey resultClass="int">SELECT @@IDENTITY As id</selectKey>    </insert>

MySql数据库创建表

3.3.MySql数据库创建表 创建5个表: UserInfo用户基础表 Role 角色表 MenuInfo 菜单即控制表 Relation_Role_Menu 角色对应菜单关系表 RelaTion_User_Role  用户角色关系表 Depart 部门信息表 表名:USERINFO 用户信息表 序号 列名 数据类型 长度 小数位 标识 主键 允许空 默认值 说明 1 ID INT 是 否 主键 2 USER_NAME VARCHAR 100 是 用户姓名 3 USER_ID VARCHAR

MySQL数据库修改表名

MySQL数据库修改表名,比如把表FaceAppVersion 修改为AppVersion,SQL如下: rename table FaceAppVersion to AppVersion; 执行如下: rename table FaceAppVersion to AppVersion > OK > 时间: 0.033s 原文地址:https://www.cnblogs.com/JohanChan/p/12058967.html

mysql数据库、表、字段、记录:增、删、改、查

数据库.表.字段.记录:增.删.改.查.分类进行学习,重点是查询. /* 数据库.表.字段.记录:增.删.改.查 */ -- 1.数据库:增删改查 create datebase if not exists jkxy; drop database if exists jkxy; rename database jkxy to jikexueyuan; --这条语句官方说不能用了 show databases; use jikexueyuan; /* 1.CREATE DATABASE new_d

MySQL数据库分表分区(一)(转)

面对当今大数据存储,设想当mysql中一个表的总记录超过1000W,会出现性能的大幅度下降吗? 答案是肯定的,一个表的总记录超过1000W,在操作系统层面检索也是效率非常低的 解决方案: 目前针对海量数据的优化有两种方法: 1.大表拆小表的方式(主要有分表和分区两者技术) (1)分表技术 垂直分割 优势:降低高并发情况下,对于表的锁定. 不足:对于单表来说,随着数据库的记录增多,读写压力将进一步增大. 水平分割 如果单表的IO压力大,可以考虑用水平分割,其原理就是通过hash算法,将一张表分为N