MySql8的简单使用(1.模糊查询 2.group by 分组 having过滤 3.JSON字段的实践)
一.like模糊查询、group by 分组 having 过滤
建表语句
create table student(id int PRIMARY KEY,name char(10),age int,sex char(5));
alter table student add height int(10);insert into student(id,name,age,sex,high) value (001,"张三",9,"男",100);
insert into student(id,name,age,sex,high) value (002,"张四",10,"男",110);
insert into student(id,name,age,sex,high) value (003,"张三淡",9,"男",120);
insert into student(id,name,age,sex,high) value (004,"李个",10,"男",10);
insert into student(id,name,age,sex,high) value (005,"刘能",10,"女",20);
insert into student(id,name,age,sex,high) value (006,"吴谷歌",20,"男",60);
insert into student(id,name,age,sex,high) value (007,"竺琦在",10,"男",80);
1.1 like 模糊查询
- 在MySQL中,like进行模糊查询。
- 通配符 % 表示任意字符序列(包括空字符),
- 通配符_ 表示任意单个字符。
//姓名包含 “三”
select * from student where name like '%三%';
//姓名 以 '张'开头
select * from student where name like '张%';
//姓名 以 '张'开头,且只有两个字
select * from student where name like '张_';
//姓名 以 '张'开头,且只有三个字
select * from student where name like '张__';
1.2 group by 分组 having 过滤
-
GROUP BY和HAVING是SQL中一起使用的两个关键字,用于对查询结果进行分组和过滤。
-
GROUP BY关键字用于将查询结果按照指定的列进行分组。它将相同值的行归为一组,并为每个组生成一个结果行。通常与聚合函数(如COUNT、SUM、AVG、MAX、MIN等)一起使用,以对每个组执行计算。
-
HAVING关键字用在GROUP BY之后对分组结果进行过滤。HAVING子句中的条件表达式只包含聚合函数,用于进一步筛选分组。
-
分组
select age,COUNT(id) as num from student group by age; -
过滤
select age,COUNT(id) as num from student group by age having num >1 and avg(high) > 100; -
首先,表中选择high >= 100的行。
然后,按照age进行分组,对每个分组计算id的数量,命名为num。
最后,使用HAVING子句筛选出满足条件num > 1的分组
select age,COUNT(id) as num from student WHERE high >= 100 group by age having num >1;
- 分组后,将一列的值聚合为一个数组
-
GROUP_CONCAT函数
select age ,GROUP_CONCAT(name ORDER BY id desc) as arrayString from student group by age;
将每个分组内的name值连接成一个以逗号分隔的字符串,按id降序排列,命名为jsonArray列
-
JSON_ARRAYAGG函数
select age ,JSON_ARRAYAGG(name ) as jsonArray from student group by age;
将每个分组内的name值连接成JSON数组,命名为jsonArray列
二.MySql8中JSON类型的使用
2.1 JSON对象
2.1.0 建表,类型选择 json
CREATE TABLE `orders` (`order_id` int NOT NULL,`customer_name` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,`order_date` date DEFAULT NULL,`order_items` json DEFAULT NULL,PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;
2.1.1插入数据
---两种插入JSON都可以
INSERT INTO `orders` VALUES (1, 'John', '2021-01-01', '{\"item1\": \"book\", \"item2\": \"pen\", \"item3\": \"pencil\"}');
INSERT INTO `orders` VALUES (2, 'Norn', '2021-01-01', '{\"item1\": {\"item1\": \"book2\", \"item2\": \"pe2n\", \"item3\": \"pencil2\"}, \"item2\": \"pe2n\", \"item3\": \"pencil2\"}');
INSERT INTO `orders` VALUES (3, 'Noo', '2021-01-01', '{"item1": "book", "item2": "pen", "item3": "pencil"}');
2.1.2 json对象的增删改查
---json对象,KEY作为查询条件,调用: $.item1.item2
select * from orders where order_items ->>'$.item2' ='pen';
select * from orders where order_items ->>'$.item1.item2' ='pe2n';select * from orders where order_id = 1;---json对象:新增 KEY
UPDATE orders
SET order_items = JSON_SET(order_items, '$.item4', 'new_item')
WHERE order_id = 1;---json对象:修改 KEY
UPDATE orders
SET order_items = JSON_SET(order_items, '$.item4', 'update_value')
WHERE order_id = 1;---json对象:删除 KEY
UPDATE orders
SET order_items = JSON_REMOVE(order_items, '$.item4')
WHERE order_id = 1;
2.2 JSON 数组
2.2.0 建表,选择 json类型
CREATE TABLE my_table (id INT PRIMARY KEY,json_array JSON
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;
2.2.1 插入数据
INSERT INTO my_table (id, json_array)VALUES (1, '[1, 2, 3, 4, 5]');
INSERT INTO my_table (id, json_array)VALUES (2, '[11, 12, 13, 14, 15]');
2.2.2 增删改查
SELECT id,json_array FROM my_table WHERE id = 1;---在JSON数组的指定位置添加一个元素:UPDATE my_table
SET json_array = JSON_ARRAY_INSERT(json_array, '$[0]', 13)
WHERE id = 1;---JSON数组:末尾添加一个元素6UPDATE my_table
SET json_array = JSON_ARRAY_APPEND(json_array, '$', 6)
WHERE id = 1;---JSON数组:修改UPDATE my_table SET json_array = JSON_REPLACE(json_array, '$[2]', 10) WHERE id = 1;---JSON数组:删除:UPDATE my_table
SET json_array = JSON_REMOVE(json_array, '$[2]') WHERE id = 1;
2.2.3 JSON数组作为查询条件
作为查询条件使用 JSON_CONTAINS函数,2种写法
- JSON_CONTAINS(json_array, ‘2’)
- JSON_CONTAINS(json_array, ‘[2, 6]’)
- 返回数组的元素:JSON数组中索引为0的元素
JSON_EXTRACT(json_array, ‘$[0]’) - 示例
-
查询JSON数组中同时包含1和2的记录,返回id,json_array
SELECT id, json_array FROM my_table WHERE JSON_CONTAINS(json_array, ‘[1, 2]’); -
查询JSON数组包含值2的记录,返回id,数组中索引为0的元素
SELECT id, JSON_EXTRACT(json_array, ‘$[0]’) AS first_element FROM my_table WHERE JSON_CONTAINS(json_array, ‘2’);
2.3 SpringBoot集成MyBatis操作MySql8的JSON类型
SpringBoot集成MyBatis操作MySql8的JSON类型
2.3 json类型的全部sql
--------------------------------JSON--------------------
---------JSON对象--
CREATE TABLE `orders` (`order_id` int NOT NULL,`customer_name` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,`order_date` date DEFAULT NULL,`order_items` json DEFAULT NULL,PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;---两种插入JSON都可以
INSERT INTO `orders` VALUES (1, 'John', '2021-01-01', '{\"item1\": \"book\", \"item2\": \"pen\", \"item3\": \"pencil\"}');
INSERT INTO `orders` VALUES (2, 'Norn', '2021-01-01', '{\"item1\": {\"item1\": \"book2\", \"item2\": \"pe2n\", \"item3\": \"pencil2\"}, \"item2\": \"pe2n\", \"item3\": \"pencil2\"}');
INSERT INTO `orders` VALUES (3, 'Noo', '2021-01-01', '{"item1": "book", "item2": "pen", "item3": "pencil"}');---json对象,KEY作为查询条件,调用: $.item1.item2
select * from orders where order_items ->>'$.item2' ='pen';
select * from orders where order_items ->>'$.item1.item2' ='pe2n';select * from orders where order_id = 1;---json对象:新增 KEY
UPDATE orders
SET order_items = JSON_SET(order_items, '$.item4', 'new_item')
WHERE order_id = 1;---json对象:修改 KEY
UPDATE orders
SET order_items = JSON_SET(order_items, '$.item4', 'update_value')
WHERE order_id = 1;---json对象:删除 KEY
UPDATE orders
SET order_items = JSON_REMOVE(order_items, '$.item4')
WHERE order_id = 1;---------JSON 数组-------CREATE TABLE my_table (id INT PRIMARY KEY,json_array JSON
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;INSERT INTO my_table (id, json_array)VALUES (1, '[1, 2, 3, 4, 5]');
INSERT INTO my_table (id, json_array)VALUES (2, '[11, 12, 13, 14, 15]');SELECT id,json_array FROM my_table WHERE id = 1;---在JSON数组的指定位置添加一个元素:UPDATE my_table
SET json_array = JSON_ARRAY_INSERT(json_array, '$[0]', 13)
WHERE id = 1;---JSON数组:末尾添加一个元素6UPDATE my_table
SET json_array = JSON_ARRAY_APPEND(json_array, '$', 6)
WHERE id = 1;---JSON数组:修改UPDATE my_table SET json_array = JSON_REPLACE(json_array, '$[2]', 10) WHERE id = 1;---JSON数组:删除:UPDATE my_table
SET json_array = JSON_REMOVE(json_array, '$[5]') WHERE id = 1;---JSON数组:作为查询条件--- 作为查询条件使用 JSON_CONTAINS函数,2种写法
--- JSON_CONTAINS(json_array, '2')
--- JSON_CONTAINS(json_array, '[2, 6]')
--- 返回数组的元素:JSON数组中索引为0的元素
--- JSON_EXTRACT(json_array, '$[0]')--- 1.查询JSON数组中同时包含1和2的记录,返回id,json_array
SELECT id, json_array FROM my_table WHERE JSON_CONTAINS(json_array, '[1, 2]');--- 2.查询JSON数组包含值2的记录,返回id,数组中索引为0的元素
SELECT id, JSON_EXTRACT(json_array, '$[0]') AS first_element FROM my_table WHERE JSON_CONTAINS(json_array, '2');