在使用数据库的时候一般都是查询居多
排序 order by
order by 默认是从小到大排, 可以添加 desc 参数,让查询结果从大到小排, 如:
mysql> select * from python order by id ;
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 2 | bb | 0 |
| 3 | cc | 0 |
| 4 | dd | 0 |
+----+---------+--------+
4 rows in set (0.00 sec)
mysql> select * from python order by id desc;
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 4 | dd | 0 |
| 3 | cc | 0 |
| 2 | bb | 0 |
| 1 | pycharm | 1 |
+----+---------+--------+
4 rows in set (0.00 sec)
在使用desc参数后我们可以明显发现查询结果的顺序已经是反过来了,也就是说,desc 可以让 order by 排序从大到小进行排序
聚合函数 count avg max min sum
聚合函数一般是用在字段中的,也有是用在整条数据上的,如:count
统计该数据表有多少条数据
mysql> select count(*) from python order by id desc;
+-----------+
| count(*) |
+-----------+
| 4 |
+-----------+
1 row in set (0.10 sec)
求平均值
mysql> select avg(id) from python order by id desc;
+---------+
| avg(id) |
+---------+
| 2.5000 |
+---------+
1 row in set (0.10 sec)
求和:
mysql> select sum(id) from order by id desc;
+---------+
| sum(id) |
+---------+
| 10 |
+---------+
1 row in set (0.00 sec)
最大,最小:
mysql> select max(id) from python order by id desc;
+---------+
| max(id) |
+---------+
| 4 |
+---------+
1 row in set (0.00 sec)
mysql> select min(id) from python order by id desc;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
可以看到,聚合函数是可以和order by 排序 配合使用的
分组 group by
group by 是给字段进行分组的, 如:
mysql> select is_del from python group by is_del;
+--------+
| is_del |
+--------+
| 1 |
| 0 |
+--------+
2 rows in set (0.00 sec)
给 is_del 字段进行分组, 相当与去重,查询该字段的唯一值
group by + group_concat 可以在分组的同时输出其他字段的值, 如:
mysql> select is_del, group_concat(name), group_concat(id) from python group by is_del;
+--------+--------------------+------------------+
| is_del | group_concat(name) | group_concat(id) |
+--------+--------------------+------------------+
| 0 | bb,cc | 2,3 |
| 1 | pycharm,dd | 1,4 |
+--------+--------------------+------------------+
2 rows in set (0.00 sec)
group by 也可以配合聚合函数进行所有, 如:
mysql> select is_del, group_concat(name), avg(id) from python group by is_del;
+--------+--------------------+---------+
| is_del | group_concat(name) | avg(id) |
+--------+--------------------+---------+
| 0 | bb,cc | 2.5000 |
| 1 | pycharm,dd | 2.5000 |
+--------+--------------------+---------+
2 rows in set (0.00 sec)
分页 limit
在一个数据表中,如果该表中的数据过多的时候我们可以使用limit进行分页,limit 分页使用两个参数,
开始页, 一页有多少个数据, 如:
mysql> select * from python limit 0, 2;
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 2 | bb | 0 |
+----+---------+--------+
2 rows in set (0.00 sec)
0 是开始的页数,2 是展示多少条数据
连接查询,一个完整的项目是会有很多数据表的,表与表之间组成关联性数据库
在整表与表之间的查询就是连接查询了,
MySQL中有 三种连接,内连接,左连接,右连接, 也可以称为两种,内连接,外连接
先创建golang数据表
CREATE TABLE `golang` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`py_id` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `py_id`(`py_id`) USING BTREE,
CONSTRAINT `py_id` FOREIGN KEY (`py_id`) REFERENCES `python` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of golang
-- ----------------------------
INSERT INTO `golang` VALUES (1, ‘lawang‘, 1);
给golang数据表添加一个数据,关联python中的 id 为 1 的数据
内连接:
mysql> select * from python inner join golang on golang.py_id = python.id;
+----+---------+--------+----+--------+-------+
| id | name | is_del | id | name | py_id |
+----+---------+--------+----+--------+-------+
| 1 | pycharm | 1 | 1 | lawang | 1 |
+----+---------+--------+----+--------+-------+
1 row in set (0.00 sec)
左连接:
mysql> select * from python left join golang on golang.py_id = python.id;
+----+---------+--------+------+--------+-------+
| id | name | is_del | id | name | py_id |
+----+---------+--------+------+--------+-------+
| 1 | pycharm | 1 | 1 | lawang | 1 |
| 2 | bb | 0 | NULL | NULL | NULL |
| 3 | cc | 0 | NULL | NULL | NULL |
| 4 | dd | 1 | NULL | NULL | NULL |
+----+---------+--------+------+--------+-------+
4 rows in set (0.00 sec)
右连接:
mysql> select * from python right join golang on golang.py_id = python.id;
+------+---------+--------+----+--------+-------+
| id | name | is_del | id | name | py_id |
+------+---------+--------+----+--------+-------+
| 1 | pycharm | 1 | 1 | lawang | 1 |
+------+---------+--------+----+--------+-------+
1 row in set (0.00 sec)
其实左右连接都差不多的,就是以哪边查询为做,如上面的左连接,已python数据表为主,查询左右边共有的,和左边满足条件的,右连接是以右边的数据库为主,牢记一种就可以了,
自关联
自关联就是在一个数据表中,数据记录之间自己关联自己表中的其他数据,在一种表中完成主键和外键的我们称为自关联, 自己关联自己,这种查询很简单,通过是否有外键值就可以判断该条数据是否是第一级, 拥有外将值得就是非第一级,如,查询所有第一级的数据, 假如python数据表中有 f_k 外键字段:
select * from pyton where f_k (f_k 表示外键字段) is null;
子查询
子查询就是在一个 select 语句中,嵌入了另外一个 select 语句, 这样的SQL语句嵌套就是子查询, 自查询一个是将嵌入的SQL语句的结果当做主查询的条件来使用的, 如:
mysql> select * from python where id in (select py_id from golang);
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
+----+---------+--------+
1 row in set (0.09 sec)
我们可以将子查询的数据当做主查询的条件来进行主查询了使用;