mysql
mysql开源关系数据库服务稳定,停机少,使用成本低。与其他数据库相比,安装使用简单,维护方便;同时,它使用它SQL 语言是目前使用最广泛、标准化的数据库语言。
mysql的一些概念
-
数据库(database):数据库是一些关联表的集合
-
数据表(table): 表是数据矩阵。数据库中的表看起来像一个简单的电子表。
-
主键:表示您在最重要的查询中使用的列或列集。它有一个快速查询性能的相关索引。查询性能受益于NOT NULL因为它不能包含任何优化,内容NULL值。使用InnoDB存储引擎组织存储引擎和表数据,可以根据一个或多个主键列快速搜索和排序。
主键是非空的,值不能重复。表中只能有一个主键,可以包含一个或多个字段(联合主键).
-
外键:外键用于关联表,使您能够跨表交叉引用相关数据, 外键约束有助于保持分散的数据一致性。外键只能引用外表中的列值.
建立外键的前提: 本表的列必须与外键类型相同(外键必须是外表主键)。 事件触发限制: on delete和on update , 可设参数cascade(跟随外键改变-级联), restrict(限制外观中外键的变化-严格, 父表不能删除或更新被子表引用的记录),set Null(设置空值-置空模式,允许外键字段NLL, 父表操作后,子表对应的字段被置空),set Default(设置默认值),[默认]no action mysql本来支持SET DEFAULT,但现在被innoDB拒绝了。由于MySQL不支持延迟约束检查,因此NO ACTION被视为RESTRICT。由于MySQL因此,不支持延迟约束检查NO ACTION被视为RESTRICT。 若有表a字段a_c设置外键,关联表b的主键b_id,所以表a叫子表,表b叫父表。
- 唯一键:确保相应字段中唯一的数据
- 一张表中可以有多个唯一键。
- 唯一键允许字段数据NULL,NULL可以有多个(NULL不参与比较)
- 索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构,能加快数据库的查询速度,数据库索引就是为了提高表的搜索效率而对某些字段中的值建立的目录。类似于书籍的目录。
- 建立索引的目的是加快对表中记录的搜索或排序,为表设置索引付出代价;一是增加数据库的存储空间,二是插入和修改数据(因为索引也会改变)。
- 当表很大,或者查询涉及多个表时,使用索引可以使查询速度快几千倍。
- 可以减少数据库IO成本,并且索引还可以减少数据库排序成本。
- 创建独特索引可以保证数据库表中每一行数据的独特性。
- 在使用分组和排序子句进行数据查询时,可以显著减少查询中分组和排序的时间。
-
索引类型:常用 普通(normal)、唯一(unique)、全文(fulltext) 通过建立倒排索引,可以大大提高检索效率.
倒排索引(英语:Inverted index),它也通常被称为反向索引,放置在文档或反向文档中,是一种存储在一个文档或一组文档中存储位置的索引方法。它是文档检索系统中最常用的数据结构
-
索引算法:btree(常用)、hash
-
区别:btree可用于比较操作符,like不易通配符合开头的,会多次进行io,所以效率低。 hash只能用于平等比较,没有范围比较,数据排序操作不可避免,遇到大量Hash在等值的情况下,性能不一定比较BTree高
数据库(database的操作)
-
创建:CREATE DATABASE menagerie 创建一个名字menagerie的数据库
1. CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci (设置字符集和字符序列 指同一字符集中字符之间的比较规则) 每个字符序只对应一个字符集,但一个字符集可对应多个字符序,其中一个是默认字符序列(Default Collation) MySQL中间的字符序名遵循命名惯例:从字符序对应的字符集名开始;_ci(表示大小写不敏感),_cs(表示大小写敏感)或_bin(表示按编码值比较)结束。 例如:在字符序中:utf8_general_ci下面,字符a”和“A”是等价的 2. 未指定表格的,将默认使用数据库用,数据库服务器字符集默认使用。 例如:在字符序中:utf8_general_ci下面,字符a”和“A”是等价的 2. 未指定表格的,将默认使用数据库用,数据库服务器字符集默认使用。
-
删除:DROP {DATABASE | SCHEMA} [IF EXISTS] menagerie 删除menagerie数据库中的所有表都被删除,其中[IF EXISTS]防止数据库不存在时出现错误。
表(table的操作)
1. 数据类型
-
数值类型
- 整数类型(精确值) [外链图片存储失败,源站可能有防盗链机制,建议保存图片直接上传(img-ABsar8VR-1652843580467)(img.png)]
- 定点类型(精确值) 存储准确的数字数据值 。使用这些类型,如货币数据,以保持准确性非常重要。
-
DECIMAL和NUMERIC 类型
在 MySQL 中,NUMERIC被实现为DECIMAL,所以关于DECIMAL的用法同样适用于 NUMERIC. 例如:DECIMAL(5,2) 这意味着小数点后两位的五位数 5是精度,范围为1-65,2是小数点后的位数,应小于或等于精度。
- 浮点类型(近似值)
-
FLOAT(单精度),DOUBLE(双精度)
MySQL 对单精度值使用四个字节,对双精度值使用八个字节。 SQL标准允许FLOAT在括号中的关键字后面的位中选择性地指定精度(但不是指数的范围),但精度值仅用于确定存储大小。精度从0到23会产生4字节的单精度列。从24到53,精度会产生8字节的双精度列。 可定义为FLOAT(M,D)、DOUBLE(M,D),m代表宽度,d代表小数位数(8).0.17开始不推荐使用,后期版本可以删除,最好不要使用) 如果设的值超出定义范围,则将进行选择(科学计数法)。 浮点值受平台或依赖性的影响
- 位值类型
-
BIT
可定义BIT(M),m以二进制储存范围1-64,b'value' ,设置值,如果值比定义的位数少,将首先补0, 例如,如果定义为BIT(6),设置值为b实际与,实际与b'000101'相同。
-
日期和时间
- DATE 范围是 '1970-01-01’to ‘9999-12-31’
- DATETIME[(fsp)] 范围是 '1970-01-01 00:00:00.000000’to ‘9999-12-31 23:59:59.999999’ fsp–0-6 默认0,微秒位数
- TIMESTAMP[(fsp)] 范围是’1970-01-01 00:00:01.000000’UTC 到’2038-01-19 03:14:07.999999’UTC 0代表’0000-00-00 00:00:00’
- TIME[(fsp)] 范围为838:59:59.000000’ 到’838:59:59.000000’ fsp–0-6 默认6,微秒位数
- YEAR[(4)] 4 位数格式的年份 显示为 1901到2155或 0000
SUM()和AVG() 聚合函数不能适用于时间值,需要计算时需要转换为数值, 例如:SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name; SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;
-
字符串类型
- CHAR和VARCHAR [外链图片存储失败,源站可能有防盗链机制,建议保存图片直接上传(img-wthHqhmm-1652843580469)(img_1.png)]
- BINARY和VARBINARY [外链图片存储失败,源站可能有防盗链机制,建议保存图片直接上传(img-3QCvMlD9-1652843580470)(img_2.png)]
- BLOB和TEXT [外链图片转失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P1nL3mjw-1652843580470)(img_3.png)]
- ENUM和SET [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K7wUxRoR-1652843580470)(img_4.png)]
BLOB和TEXT列只对行大小限制贡献9到12个字节,因为它们的内容与行的其余部分分开存储。 MySQL每个表的硬限制为4096列,但对于给定的表,有效最大值可能更小。确切的列限制取决于几个因素:
- 表的最大行大小限制列的数量(也可能是size),因为所有列的总长度不能超过此大小。
- 各列的存储要求限制了适合给定最大行大小的列数。某些数据类型的存储要求取决于存储引擎,存储格式和字符集等因素。
- 存储引擎可能会施加额外的限制来限制表列数。例如, InnoDB每个表的限制为1017列。
即使存储引擎能够支持更大的行,MySQL表的内部限制最大行大小为65,535字节。
-
json类型
JSON格式有以下的优点:
- 自动验证。错误的JSON格式会报错。
- 存储格式优化。数据保存为二进制格式,文件存储很紧凑,读取速度快。
- MySQL可以通过键或数组索引查询和修改对应的值,不用把整个字符串都读出来。
另外json还有其他需要注意的:
- JSON格式需要的磁盘空间和longblob或longtext差不多。
- JSON格式的默认值只能是null。
- JSON格式的列不能直接建立索引,可以建立JSON索引。
- JSON格式的key必须是字符串格式。value可以是字符串,数字,布尔型。
- JSON格式默认使用utf8mb4字符集,utf8mb4-bin排序,其他字符集使用JSON格式需要做字符集转换。ascii或utf8不用转换,他们是utf8mb4的子集。
- 大小写敏感,而且true,false,null这些关键字在JSON格式里都必须小写。说白了就是:null,Null,NULL,都是null,但是"Null"无法转成null,只有"null"才能转成null。
2. 表的操作
-
创建:CREATE TABLE table_name (col_1 INT(2), col_2 CHAR(2) not null, col_3 VARCHAR(20), col_4 TEXT(20), col_5 DATE, col_6 JSON,col_7 INT PRIMARY KEY (
col_1
), foreign key(col_7) references my_tab2(主键字段名) on delete cascade on update cascade, unique key(col_7));创建表,并设置主键、外键、唯一键 ,唯一键和主键也可以在定义数据类型时直接在后方指定如:
CREATE TABLE table_name (col_1 INT(2) PRIMARY KEY, col_2 CHAR(2) not null, col_3 VARCHAR(20), col_4 TEXT(20), col_5 DATE, col_6 JSON,col_7 INT unique);
-
修改: 可以修改表中字段属性,也可以再已经有表的时候添加键和索引或者删除
- 向表中添加唯一索引 alter table table_name add unique [index_name] (col_name); create unqiue index index_name on table_name(col_name) 其他索引类型也可以添加,替换关键词unique/fulltext/index(普通型)
- 向表中添加唯一键 alter table table_name add unique key(col_name)
- 向表中添加外键 alter table my_tab1 add [constraint 外键名] foreign key(外键字段名) references mytab2(主键字段名);
- 删除表中索引 alter table table_name drop index index_name
- 删除表中外键 alter table my_tab drop foreign key 外键名字 也可删除其他键
- 修改表名 ALTER TABLE t1 RENAME t2;
- 修改表中字段的数据类型 ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
- 向表中添加字段 ALTER TABLE t2 ADD d TIMESTAMP;
- 删除表中字段 ALTER TABLE t2 DROP COLUMN c;
- 修改表中字段保存位置 ALTER TABLE t3 MODIFY c2 INT STORAGE MEMORY;
- 复制表
假定t1有col1和col2两个字段 1. CREATE TABLE t2 (id INT AUTO_INCREMENT PRIMARY KEY) SELECT * FROM t1 ORDER BY col1, col2; 2. CREATE TABLE t2 LIKE t1; ALTER TABLE t2 ADD id INT AUTO_INCREMENT PRIMARY KEY; INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2 以上两种方法都能新建一个表t2,与t1完全相同,并多出一个自增主键。
-
删除: 删除表 DROP TABLE t1;
表中数据
- 插入数据
-
INSERT INTO tab_name VALUES (‘Puffball’,‘Diane’,‘hamster’,‘f’,‘1999-03-30’,NULL);
INSERT INTO `sm_article_201910_201912` VALUES ( '[]', '[{\"insert\": \"《自然》(20181213出版)一周论文导读\\n\", \"attributes\": {\"header\": 2}}, {\"insert\": \" 宗华 \"}, {\"insert\": \" 科学网 \", \"attributes\": {\"link\": \"javascript:void(0);\"}}, {\"insert\": \" \"}, {\"insert\": \"科学网\", \"attributes\": {\"bold\": true}}, {\"insert\": \" \"}, {\"insert\": {\"image\": \"\"}, \"attributes\": {\"alt\": \"\"}}, {\"insert\": \"微信号 sciencenet-cas\\n功能介绍 科学网公众号(简称“科学号”)是中国科学报社旗下面向科教界专业人群的公众号。报社同时出版有《中国科学报》《科学新闻》杂志,运营全球最大华人科学社区——科学网(www.sciencenet.cn)。\\n \"}, {\"insert\": \"昨天\", \"attributes\": {\"italic\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"Nature\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \", 13 December 2018, Volume 564 Issue 7735\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"《自然》\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"2018年12月13日第7735期564卷\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\\n\"}, {\"insert\": {\"image\": \"https://mmbiz.qpic.cn/mmbiz_png/5DZAKbdPGFesJia9ttKtVQvZOz8HPa3A2nzRpjZegsGeT2zq8PIFOL879qtyeMaSOqLJsyWFXpLnIwbUUXb2muA/640?wx_fmt=png\"}, \"attributes\": {\"align\": \"center\"}}, {\"insert\": \"\\n\\n\", \"attributes\": {\"align\": \"center\"}}, {\"insert\": \"封面图片来源:MuraliKrishna/IQOQI\", \"attributes\": {\"bold\": true, \"align\": \"right\", \"color\": \"#888888\"}}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"right\"}}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"物理学\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"Physics\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"An entanglement-based wavelength-multiplexed quantum communication network\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"基于纠缠的波长复用量子通信网络\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ 作者:Sören Wengerowsky, Siddarth Koduru Joshi, Fabian Steinlechner, et al.\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\\nhttps://www.nature.com/articles/s41586-018-0766-y\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n量子密匙分配达到的成熟度已能满足应用于现实场景的要求。这种纠缠被复用成12个波长通道。随后,在一个完全连接的图像中,6种状态在4个用户之间成功分发,而每个用户仅利用了1根
光纤和1个偏振分析模块。\\n\\n\"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nQuantum key distribution has reached the level of maturity required for deployment in real-world scenarios2,3,4,5,6.Six states are then distributed between four users in a fully connected graph using only one fibre and one polarization analysis module per user.\\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"Experimental realization of on-chip topological nanoelectromechanical metamaterials\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"实验实现芯片上拓扑纳机电超材料\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:Jinwoong Cha, KunWoo Kim & Chiara Daraio\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"https://www.nature.com/articles/s41586-018-0764-0\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n引导波通过稳定的物理信道对于可靠的信息传输非常重要。"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nGuiding waves through a stable physical channel is essential for reliable information transport."}, {\"insert\": \" \\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"化学\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"Chemistry\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"Reversible superdense ordering of lithium between two graphene sheets\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"两个石墨烯片之间锂的可逆超致密排序\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:Matthias Kühne, Felix Börrnert, Sven Fecher, et al\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"https://www.nature.com/articles/s41586-018-0754-2\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n很多碳的同素异形体能充当可逆锂吸收的主体材料,因此为现有和未来的电化学能量储存奠定了基础。"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nMany carbon allotropes can act as host materials for reversible lithium uptake, thereby laying the foundations for existing and future electrochemical energy storage."}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"Catalytic deracemization of chiral allenes by sensitized excitation with visible light\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"通过可见光敏化激活使手性丙二烯催化去外消旋\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:AlenaHölzl-Hobmeier, Andreas Bauer, Alexandre Vieira Silva, et al.\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"https://www.nature.com/articles/s41586-018-0755-1\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n手性化合物作为彼此的不重叠镜像的对映异构物存在。\\n\\n由于对映体纯手性化合物的重要性,比如作为药物活性成分,外消旋酒石酸盐(对映异构体的1:1混合物)的分离被广泛实施。"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nChiral compounds exist as enantiomers that are non-superimposable mirror images of each other. "}, {\"insert\": \" \\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"气候科学\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"Climate Science\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"Increased variability of eastern Pacific ElNiño under greenhouse warming\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"温室气体增温背景下太平洋东部厄尔尼诺变化性增加\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:Wenju Cai, Guojian Wang, Boris Dewitte, et al.\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"https://www.nature.com/articles/s41586-018-0776-9\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n厄尔尼诺—南方涛动(ENSO)是地球上占主导地位且影响最深远的气候变化。}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nThe ElNiño–Southern Oscillation (ENSO) is the dominant and most consequential climate variation on Earth, and is characterized by warming of equatorial Pacific sea surface temperatures (SSTs) during the El Niño phase and cooling during the LaNiña phase. ENSO events tend to have a centre—corresponding to the location of the maximum SST anomaly—in either the central equatorial Pacific (5° S–5° N, 160° E–150° W) or the eastern equatorial Pacific (5° S–5° N, 150°–90° W); these two distinct types of ENSO event are referred to as the CP-ENSO and EP-ENSO regimes, respectively. \\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"Widespread but heterogeneous responses of Andean forests to climate change\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"安第斯山脉森林对气候变化作出普遍但多样化的响应\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:Belén Fadrique, Selene Báez, Álvaro Duque, et al.\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"https://www.nature.com/articles/s41586-018-0715-9\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n全球变暖正在迫使很多物种向上平移其生存范围,导致特定位置的物种组成随之变化。这一预测很大程度上未在热带森林中得到验证。\\n\\n这里,我们利用一个关于近200个安第斯森林地块的数据库证实,热带和亚热带森林群落在组成上正经历方向性改变,即来自海拔较低、更温暖地域的物种相对丰度增加。\\n\\n这些地块分布在超过33.5个纬度(从南纬26.8°到北纬7.1°)和海拔相差3000米(从超过海平面360米到3360米)的范围内。"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nGlobal warming is forcing many species to shift their distributions upward, causing consequent changes in the compositions of species that occur at specific locations. This prediction remains largely untested for tropical trees. Here we show, using a database of nearly 200 Andean forest plot inventories spread across more than 33.5° latitude (from 26.8° S to 7.1° N) and 3,000-m elevation (from 360 to3,360 m above sea level), that tropical and subtropical tree communities are experiencing directional shifts in composition towards having greater relative abundances of species from lower, warmer elevations. Although this phenomenon of ‘thermophilization’ is widespread throughout the Andes, the rates of compositional change are not uniform across elevations.\\n\"}, {\"insert\": \" \\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"生物学\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"Biology\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"A circuit fromhippocampal CA2 to lateral septum disinhibits social aggression\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"从海马体CA2到侧间隔的回路使社会性攻击摆脱抑制\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:Felix Leroy, JungPark, Arun Asok, David H. Brann, et al.\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\\nhttps://www.nature.com/articles/s41586-018-0772-0\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n尽管海马体已知对陈述性记忆很重要,但海马体输出如何调控社会性攻击等动机行为仍不太明确。\\n\\n这里,我们报告称,海马体CA2区域中对社会记忆非常重要的椎体神经元,会促进小鼠的社会性攻击。这一行为取决于来自CA2的输出向即将发起攻击前选择性得到增强的侧间隔传递。\\n\\nCA2对椎体神经元的激活招募了一个使已知触发攻击的腹内侧下丘脑亚核团摆脱抑制的回路。\\n\\n\"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\nAlthough the hippocampus is known to be important for declarative memory, it is less clear how hippocampal output regulates motivated behaviours, such as social aggression. Here we report that pyramidal neurons in the CA2 region of the hippocampus, which are important for social memory, promote social aggression in mice. This action depends on output from CA2 to the lateral septum, which is selectively enhanced immediately before an attack. Activation of the lateral septum by CA2 recruits a circuit that disinhibits a subnucleus of the ventromedial hypothalamus that is known to trigger attack.\\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"Single-cell mapping of lineage and identity in direct reprogramming\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": \"直接重编程中对谱系和身份的单细胞描绘\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"作者:Brent A. Biddy, Wenjun Kong, Kenji Kamimoto, et al.\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"▲ \", \"attributes\": {\"bold\": true, \"color\": \"#888888\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"链接:\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"https://www.nature.com/articles/s41586-018-0744-4\", \"attributes\": {\"bold\": true, \"color\": \"#888888\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"▲ 摘要:\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\", \"background\": \"#f7f7f7\"}}, {\"insert\": \"\\n直接谱系重编程涉及细胞身份的转换。单细胞技术可用于解构谱系转换期间出现的大量异质性。\\n\\n\"}, {\"insert\": \"▲ Abstract\", \"attributes\": {\"bold\": true, \"color\": \"#7b0c00\"}}, {\"insert\": \"\\nDirect lineage reprogramming involves the conversion of cellular identity. Single-cell technologies are useful for deconstructing the considerable heterogeneity that emerges during lineage conversion. \\n\"}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"justify\"}}, {\"insert\": \"(宗华)\", \"attributes\": {\"bold\": true, \"align\": \"right\", \"color\": \"#888888\"}}, {\"insert\": \"\\n\", \"attributes\": {\"align\": \"right\"}}, {\"insert\": \"\\n\\n\"}, {\"insert\": \"注意!微信又双叒叕更新了......\", \"attributes\": {\"bold\": true, \"color\": \"#a70028\"}}, {\"insert\": \"\\n\\n此次改版后,每个用户最多可以设置12个常读订阅号,这些订阅号将以往常的大图封面展示。为了不错过科学网的推送,请根据以下操作,将我们“星标”吧!\\n\\n点击“\"}, {\"insert\": \"科学网\", \"attributes\": {\"bold\": true, \"color\": \"#a70028\"}}, {\"insert\": \"”进入公号页面→点击右上角的\"}, {\"insert\": \" ··· \", \"attributes\": {\"bold\": true, \"color\": \"#a70028\"}}, {\"insert\": \"菜单 →选择“\"}, {\"insert\": \"设为星标\", \"attributes\": {\"bold\": true, \"color\": \"#a70028\"}}, {\"insert\": \"”,搞定!\\n\"}, {\"insert\": {\"image\": \"https://mmbiz.qpic.cn/mmbiz_jpg/5DZAKbdPGFesJia9ttKtVQvZOz8HPa3A2FQ70KTfgpCV2ZbicpqubpTlia3T8KPSTQrq5SrruOcg2b1z9dTYiacxFA/640?wx_fmt=jpeg\"}}, {\"insert\": \"\\n\"}, {\"insert\": {\"image\": \"https://mmbiz.qpic.cn/mmbiz_jpg/5DZAKbdPGFesJia9ttKtVQvZOz8HPa3A2FH3TbXAIowExKckvXKJNG5LVMzv2YPDPuiaLAoibsh2l8oHSASHKO5TA/640?wx_fmt=jpeg\"}}, {\"insert\": \"\\n\"}, {\"insert\": {\"image\": \"https://mmbiz.qpic.cn/mmbiz_jpg/5DZAKbdPGFesJia9ttKtVQvZOz8HPa3A2yns1KOGpUSNOTdXBCzxQKaPdjkUUZm1QJH1ianeS0M2DhaKp9LHMSZA/640?wx_fmt=jpeg\"}}, {\"insert\": \"\\n\"}, {\"insert\": {\"image\": \"https://mmbiz.qpic.cn/mmbiz/5DZAKbdPGFfLHFRyG8jqf0BssMt7PCcQMcFssib5uIxt0QpbPLZ5n8HCq9WmzQtYfJm5sKegC4dibBiax585hiccvQ/640?wx_fmt=gif\"}, \"attributes\": {\"width\": \"600px\"}}, {\"insert\": \"\\n\"}, {\"insert\": \"关注我们\", \"attributes\": {\"bold\": true, \"size\": \"18px\", \"align\": \"center\", \"blockquote\": true}}, {\"insert\": \"\\n\", \"attributes\": {\"bold\": true, \"align\": \"center\", \"blockquote\": true}}, {\"insert\": \"微信号\", \"attributes\": {\"bold\": true, \"size\": \"18px\", \"color\": \"#000000\"}}, {\"insert\": \":\", \"attributes\": {\"bold\": true, \"size\": \"18px\", \"color\": \"#382727\"}}, {\"insert\": \"sciencenet-cas \", \"attributes\": {\"size\": \"18px\"}}, {\"insert\": \"(←长按复制) 或长按下方二维码\", \"attributes\": {\"bold\": true, \"size\": \"18px\", \"color\": \"#d92142\"}}, {\"insert\": \"\\n\", \"attributes\": {\"size\": \"18px\", \"color\": \"#ff0000\"}}, {\"insert\": {\"image\": \"https://mmbiz.qpic.cn/mmbiz_jpg/5DZAKbdPGFfgMHBHNib70cicZibibrvXqD6TjWVFxl54dfY1icEsxCgeicicBvCwcSZibLxPLWqfribZB8s6exeqlJ4LcFQ/640?wx_fmt=jpeg\"}, \"attributes\": {\"width\": \"auto\"}}, {\"insert\": \"\\n var first_sceen__time = (+new Date()); if (\\\"\\\" == 1 && document.getElementById(\'js_content\')) { document.getElementById(\'js_content\').addEventListener(\\\"selectstart\\\",function(e){ e.preventDefault(); }); } (function(){ if (navigator.userAgent.indexOf(\\\"WindowsWechat\\\") != -1){ var link = document.createElement(\'link\'); var head = document.getElementsByTagName(\'head\')[0]; link.rel = \'stylesheet\'; link.type = \'text/css\'; link.href = \\\"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/winwx427fda.css\\\"; head.appendChild(link); } })(); \"}, {\"insert\": {\"image\": \"\"}, \"attributes\": {\"alt\": \"\"}}, {\"insert\": \" 宗华\\n\"}, {\"insert\": \"赞赏\", \"attributes\": {\"link\": \"##\"}}, {\"insert\": \"\\n 长按二维码向我转账\\n \"}, {\"insert\": {\"image\": \"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/pic/appmsg/pic_reward_qrcode.2x3534dd.png\"}}, {\"insert\": \"受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。\\n阅读 好看 \"}, {\"insert\": \"已推荐到看一看\", \"attributes\": {\"bold\": true}}, {\"insert\": \"\\n\"}, {\"insert\": {\"image\": \"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/pic/appmsg/pic_haokan427eca.png\"}}, {\"insert\": \" 你的朋友可以在“发现”-“看一看”看到你认为好看的文章。\\n\"}, {\"insert\": \"知道了\", \"attributes\": {\"link\": \"javascript:;\"}}, {\"insert\": \"\\n 已推荐到看一看 \"}, {\"insert\": \"和朋友分享想法\", \"attributes\": {\"link\": \"javascript:;\"}}, {\"insert\": \"\\n最多200字,当前共字 发送\\n 已发送\\n\"}, {\"insert\": \"朋友将在看一看可见\\n\", \"attributes\": {\"header\": 4}}, {\"insert\": \"确定\\n 分享你的想法... \\n取消\"}, {\"insert\": \"分享想法到看一看\\n\", \"attributes\": {\"header\": 4}}, {\"insert\": \"确定\\n\\n最多200字,当前共字', 0, 2, 9, 0, '[]', '2018-12-16 08:00:00', NULL, '[]', '755870f4-6365-4a15-81e3-ae8c740254e2', 'cc3798de-73a4-4acc-b58b-13cdf5502af4', 'cc3798de-73a4-4acc-b58b-13cdf5502af4', '2019-12-17 00:29:41', '2022-02-26 11:06:31', NULL);
其中VALUES括号中的内容要按照表的定义添加数据类型,并用逗号分隔,
当插入的数据类型有json时,key一定要是字符串
-
查询数据
查询总体语法为
SELECT what_to_select 查询的字段
FROM which_table 表名
WHERE conditions_to_satisfy; 条件
-
全部查询
当将where条件语句删除后,即为查询该表中所有数据 当查询的字段变为*时,即查询表的所有字段。如果只查询多个字段,字段之间使用逗号隔开 当在查询的字段前添加DISTINCT关键词,查询结果会去重。
-
条件查询
where可以接查询条件如:id=‘1’ 即查询表中id=‘1’的数据
-
修改数据
UPDATE 表名 SET 字段 = value1 WHERE 字段 = value2;
-
删除数据
DELETE FROM table_name [WHERE Clause]
如果没有指定where子句的条件,将删除表内所有数据
-
where子句 可包含下列操作符
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DqJWsrTd-1652843580471)(img_5.png)]
当使用NULL时,不能使用上述操作符,应使用 is null ,is not null
也可以是多个条件并列查询,使用AND连接两个条件,例如:
SELECT * FROM pet WHERE species = ‘dog’ AND sex = ‘f’;
也可以是多个条件满足其一,使用OR连接,例如:
SELECT * FROM pet WHERE species = ‘snake’ OR species = ‘bird’;
还可以混合使用AND和OR,例如:
SELECT * FROM pet WHERE (species = ‘cat’ AND sex = ‘m’) OR (species = ‘dog’ AND sex = ‘f’);
-
排序 ORDER BY
SELECT name, birth FROM pet ORDER BY birth ASC升序默认 /DESC降序
当order by一个字符字段时,顺序是不一定的,可以强制使用二进制排序,例如:
ORDER BY BINARY col_name
排序也可按照多个字段、多个顺序排序,例如:
SELECT name, species, birth FROM pet ORDER BY species, birth DESC;
-
函数
提供了一些函数来进行计算
-
TIMESTAMPDIFF(date1,date2),计算两个时间差多少
SELECT name, birth, CURDATE(), TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age FROM pet // 计算宠物到现在多少岁,并将时间差值命名为age,这个age字段也可以用来进行排序使用。
-
MONTH(date) 获取日期的月份
-
MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH)) 在当前日期上添加一个月的时间间隔,然后获取月份
-
MOD(MONTH(CURDATE()), 12) + 1;也能达成上个计算结果。//mod(n,m)返回N除以M的余数
-
COUNT() // 计数,符合条件的有多少条,计数为多少 ,改用id,不使用
-
AVG(expression) 求平均值
-
MAX(expression) 取最大值
-
MIN(expression) 取最小值
-
SUM(expression) 求和
-
-
模糊查询 LIKE
使用“%”代表通配符,可进行模糊查询,例:
SELECT * FROM pet WHERE name LIKE ‘b%’;
SELECT * FROM pet WHERE name LIKE ‘_____’// 查询name是五个字符的数据
-
分组 Group by
SELECT column_name, function(column_name)
FROM table_name
GROUP BY column_name
也可按照多字段分组,字段间用逗号隔开
-
分页 limit关键词
select * from table_name limit 0,5 ,查询前五条 5是pageSize
-
多表查询
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5d0xzQLV-1652843580471)(img_6.png)]
SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species FROM pet AS p1 INNER JOIN pet AS p2 ON p1.species = p2.species AND p1.sex = ‘f’ AND p1.death IS NULL AND p2.sex = ‘m’ AND p2.death IS NULL
-
in 从句
- select xxx from table_name where column_name in (select xxx from xxx);
- select xxx from table_name where (column1_name, column2_name) in (select xxx from xxx);
-
exists 从句
-
select xxx from table_name where exists (subquery);
与in从句截然不同的是 写在圆括号内的内查询并不返回一张表 而是返回一个布尔值 (true 或者 false)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9nekMtVc-1652843580472)(img_8.png)]
-
-
having
- Where是一个约束声明,在查询数据库的结果返回之前对数据库中的查询条件进行约束,即在结果返回之前起作用,且where后面不能使用聚合函数,因为where的执行顺序在聚合函数之前。
- Having是一个过滤声明,所谓过滤是在查询数据库的结果返回之后进行过滤,即在结果返回之后起作用,并且having后面可以使用聚合函数。不能对没有查出来的值使用having。 但是where和having可以混用
select sum(score) from student where sex=‘man’ group by name having sum(score)>210;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EGhcy7SN-1652843580472)(img_9.png)]
用户定义变量
定义的用户变量不再是JSON格式,而是String格式,这个是在赋值给变量的时候转的。另外,变量的字符集和排序规则和JSON格式相同:utf8mb4和utf8mb4_bin
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qpuOU8oC-1652843580472)(img_49.png)]
json路径表达
MySQL用路径表达式对JSON格式的数据进行查询。
- 路径表达式中用$代表JSON值。
- 用key值代表该key对应的元素。
- 用[N]代表JSON数组中的第N个元素。序号从0开始。
- 用[M to N]代表JSON数组中第M个至第N个元素。序号从0开始。
- 用.[*]通配符代表JSON对象的所有子元素。
- 用[*]通配符代表JSON数组的所有元素。
- 用**通配符代表用某字符开头或结尾的元素。
例:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yoIljPNa-1652843580473)(img_50.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QhwU7fXb-1652843580473)(img_52.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xxZnZ8tH-1652843580473)(img_53.png)]
json方法
1. JSON_MERGE_PATCH(json_doc, json_doc[, json_doc] …) 合并
1. 如果第一个参数不是对象,那么就和一个空对象与第二个参数合并是一样的效果
2. 如果第二个参数不是对象,那么结果就是第二个参数
3. 如果所有参数都是对象,则合并结果满足下列情况
- 结果包含第一个对象中不存在于第二个对象中的key
- 结果包含第二个对象中不存在于第一个对此昂中且不是null的key。
- 如果key同时存在于两个对象中且key的值是对象,且第二个对象中此属性不是null,那么结果则是这两个对象中的属性递归合并值。
- 如果key同时存在于两个对象中且key的值不是对象,且第二个对象中此属性不是null,那么结果是第二个对象中这个key的值。
例:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8q6JeBBX-1652843580473)(img_10.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P6mZsflv-1652843580474)(img_11.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nahZRD8J-1652843580474)(img_12.png)]
2. JSON_MERGE_PRESERVE(json_doc, json_doc[, json_doc] …)
合并多个json,并返回合并结果,但如果有任何一个参数为null,则返回null,如果有参数不是json将会报错
1. 相邻数组合并为一个数组
2. 相邻对象递归合并为一个对象,如有相同的key,则几个value合并成一个数组作为最终的value
3. 单个值自动包装并合并为一个数组
4. 对象和数组合并,对象转为一个只有单个元素的数组,和数组合并成一个新的数组。
例:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PA9kPBlt-1652843580474)(img_13.png)]
3. JSON_ARRAY_APPEND(json_doc, path, val[, path, val] …)
将值追加到JSON文档中指定数组的末尾,并返回结果。如果任何参数为NULL,则返回NULL。如果有参数不是json或者任何路径参数不是有效参数或包含通配符(或*),将会报错
向JSON数组中追加元素,如果对应位置是单个元素,则和新元素一起封装成数组。
mysql5.7 时 JSON_APPEND(),mysql8.0后不支持这个名称
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pay0muZR-1652843580474)(img_14.png)]
4. JSON_ARRAY_INSERT(json_doc, path, val[, path, val] …)
在数组的指定位置追加元素,原位置的元素和后面的元素依次向后移一位。如果任何参数为NULL,则返回NULL。 如果有参数不是json或者任何路径参数不是有效参数或包含通配符(或*),将会报错
指定位置超过数组上限,则添加在数组最后位置。
注意:在多个位置添加多个元素时,添加是有顺序的,后面添加的元素需要等前面的元素添加成功后重新确认具体位置
例:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gDWUrI5p-1652843580475)(img_15.png)]
5. JSON_INSERT(json_doc, path, val[, path, val] …)
向JSON添加键值对。
如果添加的key已经存在,则忽略此键值对,不再添加。
如果向json数组添加一个超出下标的值,则会将值追加到数组中,如果不是向数组中添加,则会将其包装成数组,然后再扩展新的值
如果任何参数为NULL,则返回NULL。 如果有参数不是json或者任何路径参数不是有效参数或包含通配符(或*),将会报错
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q5V9LpjC-1652843580475)(img_18.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8vpOGIo0-1652843580475)(img_16.png)]
从此结果可以看到,此函数不支持数据类型转换,'[true, false]'参数被当做字符串添加了,如果需要作为JSON数组添加,需要手动转换:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TlO5tPhw-1652843580475)(img_17.png)]
6. JSON_REPLACE(json_doc, path, val[, path, val] …)
替换JSON中的值。
如果路径不存在,则忽略。
如果任何参数为NULL,则返回NULL。 如果有参数不是json或者任何路径参数不是有效参数或包含通配符(或*),将会报错
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bxbYy4nv-1652843580475)(img_19.png)]
7. JSON_SET(json_doc, path, val[, path, val] …)
替换JSON中的值。
如果路径不存在,则添加该值。
如果修改的是数组,路径超过了数组上限,则把元素添加到数组末尾。
如果任何参数为NULL,则返回NULL。 如果有参数不是json或者任何路径参数不是有效参数或包含通配符(或*),将会报错
例:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qh9yYuwg-1652843580476)(img_20.png)]
三者区别:
JSON_SET():路径存在则替换值,路径不存在则新增。
JSON_INSERT():只负责新增。
JSON_REPLACE():只负责替换已存在的值
8. JSON_REMOVE(json_doc, path[, path] …)
从JSON中删除对应路径下的元素。
指定路径不存在也不会报错。
如果任何参数为NULL,则返回NULL。 如果有参数不是json或者任何路径参数不是有效参数或包含通配符(或*),将会报错
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SqS4Ngrl-1652843580476)(img_21.png)]
9. JSON_UNQUOTE(json_val)
去掉JSON值的双引号,并返回utf8mb4格式的字符串。此函数可以识别转义字符。
如果任何参数为NULL,则返回NULL。 如果值包裹在双引号中,但不是有效的JSON字符串文本,会报错。
转义:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t8gBpGoU-1652843580476)(img_23.png)]
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1DGN0fcI-1652843580476)(img_22.png)]
10. JSON_ARRAY([val[, val] …])
根据参数值创建JSON数组,每个参数都是数组中一个元素。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iCIk7Hvn-1652843580476)(img_24.png)]
11. JSON_OBJECT([key, val[, key, val] …])
根据参数列表创建JSON对象。
按照参数的排列,第一个是key,第二个是value,第三个是key,以此类推。参数个数必须是双数。
参数列表可以为空,得到空JSON:{}。
key不能是null。否则报错。value可以是null。
数据类型错误时会报错。
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UCxhjUFO-1652843580477)(img_25.png)]
12. JSON_QUOTE(string)
用双引号把字符串括起来,把结果转为utf8mb4并返回。其中会经过JSON的转义。目的是得到JSON的字符串形式。如果参数是null,则返回null
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zQkbNcHg-1652843580477)(img_26.png)]
13. JSON_CONTAINS(target, candidate[, path])
检查候选JSON是否包含在目标JSON中,1表示包含,0表示不包含。
target是目标元素,candidate是候选元素,path是路径表达式,如果path有值,则目标元素需要先经过路径表达式的处理再参与判断。
另有如下规则:
1)参数可比时才能使用此函数。json_type()相同时才能使用此函数,另外integer和decimal类型可以直接比。
2)数组包含在数组中。只有目标数组的每个元素都包含在候选数组中,才算包含。
3)非数组包含在数组中。只有候选JSON对象包含在目标数组的某个元素中,才算包含。
4)非数组包含在非数组中。当且仅当候选JSON对象的每个key,在目标JSON对象中都有同名的key和同值的value,才算包含.
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2QexO3xM-1652843580477)(img_27.png)]
14. JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] …)
判断目标JSON中是否存在指定的路径或路径列表。
第一个参数是目标JSON。
第二个参数可以选择one或all。如果选择one,那么只要其中一条路径是存在的就返回1,否则返回0。如果选择all,那么必须所有路径都存在才返回1,否则返回0。
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z1sqj6yP-1652843580477)(img_28.png)]
15. JSON_EXTRACT(json_doc, path[, path] …)
从目标JSON中返回对应路径下的元素。如果匹配到多个元素则封装成数组。
如果有参数为null或者,路径不存在,则返回null
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qxofAZz4-1652843580478)(img_29.png)]
16. ->运算符
此运算符是JSON_EXTRACT()函数的简写,单个path的场景。
个运算符几乎可以出现在sql的所有位置,而且在select,update等语句中都能用
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wwk7Unxw-1652843580478)(img_30.png)]
17. ->>运算符
->>运算符是加强版的->运算符,他把->运算符得到的结果去掉了引号,就像JSON_UNQUOTE ()函数一样,所以,以下三个表达式所代表的含义是一样的:
JSON_UNQUOTE( JSON_EXTRACT(column, path) )
JSON_UNQUOTE(column -> path)
column->>path
和->>运算符一样,->>运算符可以被用到sql中的很多位置
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l0tB4tPr-1652843580478)(img_31.png)]
18. JSON_KEYS(json_doc[, path])
返回JSON中一级键值对中key的列表。如果写了path字段,则先进行路径表达式计算,然后的返回第一级键值对中key的列表。
也就是说,不会返回子元素的key。
如果有参数为null,或者路径不存在,或json不是一个对象,则返回null。
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fjqkwf4v-1652843580478)(img_32.png)]
19. JSON_OVERLAPS(json_doc1, json_doc2)
比较两个JSON是否有相同元素。也就是是否有交集。有则返回1,没有返回0。
部分匹配的情况不能算有相同元素。
两个JSON对象比较时,两者至少有一个相同name的key和相同对应value,则返回1。
两个标量比较时,则比较值是否相等。
标量和数组比较时,则判断标量是否和数组某元素相等。数据类型不同时不算相等。
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1d5uFP8o-1652843580478)(img_33.png)]
20. JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] …])
根据某字符串,返回在目标JSON中匹配的value的路径表达式,search_str和JSON中的value相等时算匹配。
此函数能查询子元素信息。
第一个参数json_doc是目标JSON。
第二个参数one_or_all可以选择one或者all。one表示返回一个匹配的值的路径。all表示返回所有路径。
参数search_str是要搜索的字符串。其中可以用%(百分号)代表任意多个字符,_(下划线)代表任意一个字符。
参数escape_char是转义字符。默认是\。写成空字符串或NULL时,也默认为\。
参数path是路径表达式,如果写了path,匹配结果需在路径表达式下进行。
当前三个参数任一为null、或者路径在json中不存在,或者没查找到匹配的路径,都返回null
例:
mysql> SELECT JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'one', 'abc');
+--------------------------------------------------------------------------------------+
| JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'one', 'abc') |
+--------------------------------------------------------------------------------------+
| "$[0]" |
+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', 'abc');
+--------------------------------------------------------------------------------------+
| JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', 'abc') |
+--------------------------------------------------------------------------------------+
| ["$[0]", "$[2].x"] |
+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', 'abcdefg');
+------------------------------------------------------------------------------------------+
| JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', 'abcdefg') |
+------------------------------------------------------------------------------------------+
| NULL |
+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', '10', NULL, '$[1][0].k');
+--------------------------------------------------------------------------------------------------------+
| JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', '10', NULL, '$[1][0].k') |
+--------------------------------------------------------------------------------------------------------+
| "$[1][0].k" |
+--------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', '%b%', NULL, '$[3]');
+----------------------------------------------------------------------------------------------------+
| JSON_SEARCH('["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]', 'all', '%b%', NULL, '$[3]') |
+----------------------------------------------------------------------------------------------------+
| "$[3].y" |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
21. value MEMBER OF(json_array)
判断value是否被包含在某JSON数组中。包含则返回1,否则返回0。
数据格式不同时不算包含。
value可以用其他表达式替代。
JSON格式的字符串不能直接和数组中的JSON对象比较,会返回0,此时需要把value转成JSON类型才能返回1。
例: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1Qb3SunV-1652843580479)(img_34.png)]
22. JSON_DEPTH(json_doc)
返回JSON值的最大深度。
空数组,空对象,