新建表
CREATE TABLE `user_copy` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`lastlogininfo` json DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=1;
插入数据,普通用法:
INSERT INTO user VALUES(1 ,'lucy','{"time":"2015-01-01 13:00:00","ip":"192.168.1.1","result":"fail"}');
INSERT INTO user VALUES(2 ,"bobo",'{"time":"2015-10-07 06:44:00","ip":"192.168.1.0","result":"success"}');
json_object函数用法:
INSERT INTO user VALUES(3 ,"king",JSON_OBJECT("time",NOW(),"ip","192.168.1.1","result","fail"));
json数据条件过滤
SELECT * from `user` where lastlogininfo -> '$.result' = 'fail'
SELECT * FROM `user` WHERE JSON_EXTRACT(lastlogininfo,'$.result') = 'fail';
结果都是:
其表达式为:该json列->'$.键'与JSON_EXTRACT(json列 , '$.键')等效使用。如果传入的不是一个有效的键,则返回Empty set。该表达式可以用于SELECT查询列表 ,WHERE/HAVING , ORDER/GROUP BY中,但它不能用于设置值。