MySQL cast()函数以及json列

在工作中遇到了json列,不清楚如何写SQL,查询了下相关的文档之后总结下,根据json列的值进行区分,列值指的是 json_type(json列)的结果

1、列值为NULL

create table t1(c1 int primary key, c2 json);insert into t1 values(4, NULL);
select * from t1;insert into t1 values(5, null);
select * from t1;drop table t1;

2、列值为 time/date/datetime

create table t3(c1 varchar(30) not null, j json);insert into t3 values ('time' ,cast(cast('10:10:10' as time) as json)), ('date' ,cast(cast('2010-10-10' as date) as json));
select j, json_type(j), j from t3;insert into t3 values ('time' ,cast(cast('23:24:25' as time) as json));
select j, json_type(j), j from t3;insert into t3 values ('date' ,cast(cast('2015-01-15' as date) as json));
select j, json_type(j), j from t3;insert into t3 values ('datetime' ,cast(cast('2015-01-15 23:24:25' as datetime) as json));
select c1, j, json_type(j), j from t3;

3、列值为 signed/unsigned

create table t5(c1 varchar(30) not null, j json, c2 int);insert into t5 values ('ee', cast(cast(7.50 as signed) as json), 0);
select json_type(j), j from t5;insert into t5 values ('rr', cast(cast(-1.49 as signed) as json), 1);
select json_type(j), j from t5;insert into t5 values ('dd', cast(cast(16 as signed) as json), 4);
select json_type(j), j from t5;insert into t5 values ('ee', cast(cast(17 as unsigned) as json), 5);
select json_type(j), j from t5;insert into t5 values ('ff', cast(cast(-3.8 as unsigned) as json), 6);
select json_type(j), j from t5;insert into t5 values ('ee', cast(cast(-3.8 as signed) as json), 7);
select c1, json_type(j), j from t5;

4、列值为 object

create table t2(c1 varchar(30) not null, j json);insert into t2 values ('sssssss', JSON_OBJECT("mascot", "Our mascot is a dolphin named \"Sakila\"."));
select json_type(j) from t2;insert into t2 values ('sssssss', cast('{}' as json));
select json_type(j) from t2;

5、列值为 double/float

create table t6(c1 varchar(50) not null, j json);insert into t6 values ('hh' ,cast(cast(4.6 as float) as json));
select json_type(j), j from t6;insert into t6 values ('jj' ,cast(cast(5.19 as double) as json));
select json_type(j), j from t6;insert into t6 values ('ii' ,cast(cast(100 as double) as json));
select json_type(j), j from t6;insert into t6 values ('kk' ,cast(cast(DATE'2019-08-07' as double) as json));
select json_type(j), j from t6;insert into t6 values ('ll' ,cast(cast(-1 as float) as json));
select c1, json_type(j), j from t6;

6、列值为 timestamp

create table t8(c1 varchar(30) not null, j json);insert into t8 values ('timestamp', cast(TIMESTAMP'2015-01-15 23:24:25' as json));
select c1, json_type(j), j from t8;select * from t8 where j->'$' = TIMESTAMP'2015-01-15 23:24:25';

7、列值为array

create table t9(c1 varchar(30) not null, j json);INSERT INTO t9 VALUES ('eeeeeeee', CAST('[]' AS CHAR CHARACTER SET 'ascii'));
select c1, json_type(j), j from t9;select * from t9 where j->'$' = JSON_ARRAY(1, 2, 3);

8、列值为blob

create table t10(c1 varchar(30) not null, j json);INSERT INTO t10 VALUES ('rrrrr', CAST(x'cafebabe' AS JSON));
select c1, json_type(j), j from t10;select * from t10 where j->'$' = x'cafebabe';

9、decimal

create table t12(c1 varchar(30) not null, j json);insert into t12 VALUES ('wwww', cast(cast('12.5' as decimal) AS JSON));
select json_type(j), j from t12;insert into t12 VALUES ('qqqq', cast(cast('12.5' as decimal(6,2)) AS JSON));
select json_type(j), j from t12;insert into t12 VALUES ('eeee', cast(cast('12.5' as decimal(6,0)) AS JSON));
select c1, json_type(j), j from t12;

10、string

create table t11(c1 varchar(30) not null, j json);insert into t11 VALUES ('wwww', '"scalar string"');
select json_type(j), j from t11;select * from t11 where j->'$' = "scalar string";

11、bool

create table t7(c1 varchar(30) not null, j json);insert into t7 values ('aa', 'true');
select json_type(j), j from t7;insert into t7 values ('bb', 'false');
select c1, json_type(j), j from t7;

12、null

create table t14(c1 varchar(30) not null, j json);insert into t14 values ('nn', 'null');
select c1, json_type(j), j from t14;

13、equivalent

create table tt(c1 int primary key, j json);insert into tt values (1, '"2019-1-1"');
insert into tt values (2, '"2019-01-01"');
insert into tt values (3, cast(DATE'2019-1-1' as json));select * from tt where j->'$' = '2019-1-1';
select * from tt where j->'$' = '2019-01-1';select * from tt where j->'$' = '2019-01-01';
select * from tt where j->'$' = '2019-1-01';select * from tt where j->'$' = DATE'2019-01-01';
select * from tt where j->'$' = DATE'2019-01-1';
select * from tt where j->'$' = DATE'2019-1-01';

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/462643.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算法导论13-1节习题解答

CLRS 13.1-1利用性质画图,略 CLRS 13.1-2是否 CLRS 13.1-3是,因为就根部被改变了,并不与其他性质矛盾。 CLRS 13.1-44,两个子结点都为红色3,两个子结点一红一黑2,两个子结点都为黑 树的叶子的深度将会都一样…

关于z-index的一些问题

div2的z-index为2,但是现在绝对定位的div3明明z-index比它大, 却依旧在这个层之下。原因是z-index是相对同一父元素下叠加时的z轴顺序。 z-index具有继承性,用简单的数学逻辑表示就是:div1的z-index为1,则它 的子元素d…

查询mysql单个分区的方法

os : linux 数据库: musql 8.0.25 今天工作中遇到了如何查询单个分区中数据的问题,记录下以便于后续再次遇到此问题就可以直接查询该文章了。 建表语句和插入数据的SQL语句如下: drop table if exists tt;create table tt (c1 int primary key, c2 va…

2013腾讯编程马拉松初赛(3月20日)

1 第一题 小Q系列故事——屌丝的逆袭 表示这道题基本没什么算法,学过计算机语言的应该都能搞定吧。 2 第二题 小明系列故事——买年货 这道题直接用01背包问题就可以解决了,只是除了钱的限制,还有积分的限制和免费的情况,就是这点…

MySQL中创建partition表的几种方式

OS : linux 数据库:MySQL 8.0.25 MySQL中创建partition表的几种方式如下,这几种方式都是经过验证过的,只需将enginexxx修改即可: 1. PARTITION BY RANGE drop table if exists employees;CREATE TABLE employees (id INT NOT N…

Windows跟Linux的不同处理

1. 时区 1.1 北京时间 Windows:TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"); Linux:TimeZoneInfo tzBeijing TimeZoneInfo.FindSystemTimeZoneById("Asia/Shanghai"); 1.2 美东时间 Window: TimeZoneI…

我的学习工作经历,一个园林专业中专毕业生的IT之路

魏琼东,男,1983年生人,祖籍甘肃陇南人,首先得感谢我父亲给我取了这么一个好名字,至少我非常喜欢他,因为目前还没有发现和我同名的人。 我是1998-2002年在甘肃林业学校读了四年的园林专业,那四年…

Windows 恢复环境(Windows RE模式)

Windows 恢复环境 (Windows RE) 是一个能修复无法启动操作系统的常见问题的恢复环境。Windows 预安装环境 (Windows PE) 是具有有限服务的最小 Win32 操作系统。Windows RE 建立在 Windows 预安装环境 (Windows PE) 的基础上,并且可以用附加的驱动程序、语言、Windo…

开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别

最近工作中遇到了开源许可证的问题,需要测试基于开源软件开发的本公司产品满足哪些开源协议,网上找了一些关于这方面的解答,在此备份下: 首先借用有心人士的一张相当直观清晰的图来划分各种协议:开源许可证GPL、BSD、M…

数据库知识点

1.左连接,等值连接,自然连接 等值连接:关系R、S,取两者笛卡尔积中属性值相等的元组 自然连接:是一种特殊的等值连接,它要求比较的属性列必须是相同的属性组,并且把结果中重复属性去掉。 左连接:…

SQL Server 索引结构及其使用(一)[转]

一、深入浅出理解索引结构  实际上,您可以把索引理解为一种特殊的目录。微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引、簇集索引)和非聚集索引(nonclustered index&#xff0c…

linux文件的时间格式

背景: 今天观察数据库文件的时候发现: ls -la *.ibd -rw-rw---- 1 mysql dba 98304 Sep 25 2012 a.ibd -rw-rw---- 1 mysql dba 131072 Oct 12 2012 b.ibd -rw-rw---- 1 mysql dba 98304 Oct 17 15:34 c.ibd …

生成器和推导式

def func():print(哈哈哈)yield 1 # return 和 yield 都可以返回数据print(呵呵呵) gen func() # 不会执行你的函数,拿到的是生成器 ret gen.__next__() # 会执行到下一个yield print(ret) gen.__next__() # 继续执行函数到下一个yield 函数中如果有yield 这个函…

什么是 mmap

1. mmap 基础概念 mmap 即 memory map,也就是内存映射。 mmap 是一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系。实现这样的映射关系后&#xff…

最近项目中遇到的一个算法挑战

在企业信息化进程中,各类智能化运算在不断挑战着我们,最近又遇到一个应用,归纳一下需要实现以下算法 //算法设计 /* *计算是否存在值X1,X2,...,Xm,使等式成立 m*X1(m-1)*X2(m-3)*X3...XmE *如果可解,取解集…

c#利用定时器自动备份数据库(mysql)

1:引用dll MySql.Data.dll, MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql{public static string constr "databasetest;Password密码;user IDroot;serverip地址";public static MySqlConnection conn new MySqlConnection(cons…

构建SAN网络存储

实验目的:构建SAN网络存储 实验环境:redhat 5.4三台,scanstorage作为存储服务,IP地址是:192.168.9.79。node1,node2作为客户端测试 实验步骤: 一、配置服务端: 1、添加一块20G的盘 [rootscans…

netnbsp;与nbsp;javascript脚本的几种交互方法

1.asp.net呼叫jsResponse.Write("<scriptlanguagejavascript>");Response.Write("alert(登峰欢迎您 );" );Response.Write("location.hreflogin.aspx;") ;Response.Write("</script>");在这里情况下,你可以调用页面中的JS…

win10 + 独显 + Anaconda3 + tensorflow_gpu1.13 安装教程(跑bert模型)

这里面有很多坑&#xff0c;最大的坑是发现各方面都装好了结果报错 Loaded runtime CuDNN library: 7.3.1 but source was compiled with: 7.4.1&#xff0c;这是由于最新的tensorflow1.13需要用 Cudnn7.4.1编译。这个问题&#xff0c;StackOverflow上有人问到&#xff0c;但是…

Oracle client 安装、配置

一、安装 链接: https://pan.baidu.com/s/1Yph6hiNkCJsApAzu_Vx2ew 提取码: r9ye 二、配置 1、控制面板\所有控制面板项\管理工具\数据源(ODBC) 注&#xff1a;odbc 分 64 位和 32 位的2、测试 ODBC 连接 Oracle 数据库点击 ODBC&#xff0c;在“用户 DSN”页签下点击添加按钮…