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';

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
虽然MySQL 5.7及以上版本支持JSON数据类型,但如果您使用的是MySQL 5.6或更早版本,则无法使用JSON类型。在这种情况下,您可以考虑使用`BLOB`类型存储JSON数据。 使用`BLOB`类型存储JSON数据时,您需要将JSON字符串转换为二进制数据,然后将其存储在`BLOB`中。在查询时,您需要将`BLOB`数据转换回JSON字符串,然后将其解析为JSON对象。 以下是使用`BLOB`类型存储JSON数据的示例: 首先,创建一个包含`BLOB`类型的的表: ``` CREATE TABLE mytable ( id INT(11) NOT NULL AUTO_INCREMENT, data BLOB, PRIMARY KEY (id) ); ``` 然后,将JSON字符串转换为二进制数据,并将其插入到表中: ``` INSERT INTO mytable (data) VALUES (CONVERT('{"name": "John", "age": 30}' USING utf8mb4)); ``` 在上面的示例中,我们使用`CONVERT`函数JSON字符串转换为二进制数据,并使用`utf8mb4`字符集进行编码。 最后,在查询时,我们需要将`BLOB`数据转换回JSON字符串,并将其解析为JSON对象。以下是查询并解析JSON数据的示例: ``` SELECT CAST(data AS CHAR) FROM mytable; ``` 在上面的示例中,我们使用`CAST`函数将`BLOB`数据转换回CHAR类型(即字符串),然后使用JSON函数解析JSON数据: ``` SELECT JSON_EXTRACT(CAST(data AS CHAR), '$.name') AS name FROM mytable; ``` 在上面的示例中,我们使用`JSON_EXTRACT`函数JSON对象中选择`name`属性,并将其作为新的`name`返回。 需要注意的是,使用`BLOB`类型存储JSON数据可能会对性能产生负面影响,并且在查询时需要进行额外的转换和解析。因此,如果您使用的是MySQL 5.7及以上版本,则最好使用JSON类型来存储和查询JSON数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值