MySQL 5.7.7开始,支持了JSON类型。
CREATE TABLE IF NOT EXISTS `spss`.`variable` ( `name` VARCHAR(512) NOT NULL, `type` JSON NOT NULL, `width` INT NOT NULL, `decimals` INT NOT NULL, `label` VARCHAR(512) NOT NULL, `values` JSON NOT NULL, `missing` JSON NOT NULL, `columns` INT NOT NULL, `align` ENUM(‘Left‘, ‘Right‘, ‘Center‘) NOT NULL, `measure` ENUM(‘Scale‘, ‘Ordinal‘, ‘Norminal‘) NOT NULL, `role` ENUM(‘Input‘, ‘Target‘, ‘Both‘, ‘None‘, ‘Partation‘, ‘Split‘) NOT NULL, PRIMARY KEY (`name`)) ENGINE = InnoDB; insert into variable values(‘S1‘, ‘{"Numeric":"8:0"}‘, 8, 0, ‘您的性别?‘, ‘{"1":"男","2":"女"}‘, ‘{"Missing":"None"}‘,8,‘Left‘,‘Nominal‘, ‘Input‘); insert into variable values(‘S2‘, ‘{"Numeric":"8:0"}‘, 8, 0, ‘您的实际年龄?‘, ‘{}‘, ‘{}‘,8,‘Left‘,‘Nominal‘, ‘Input‘); select * from variable where JSON_Extract( `values`,‘$."1"‘) = ‘男‘; select * from variable where `values`->‘$."1"‘ = ‘男‘; select * from variable where measure = ‘Nominal‘;
注意的几点:
ENUM类型的用法。
JSON类型的用法。
JSON类型中,关键字如果是数字:‘{"1":"男","2":"女"}‘,在查询时要用双引号把数字括起来:`values`->‘$."1"‘
列名是SQL关键词时,用“`”字符包裹列名,如上面的例子里的`values`。
本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/1945105
原文地址:http://h2appy.blog.51cto.com/609721/1945105