MySQL 5.7.7開始,支持了JSON類型。sql
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';
注意的幾點:ide
ENUM類型的用法。orm
JSON類型的用法。ci
JSON類型中,關鍵字若是是數字:'{"1":"男","2":"女"}',在查詢時要用雙引號把數字括起來:`values`->'$."1"'get
列名是SQL關鍵詞時,用「`」字符包裹列名,如上面的例子裏的`values`。it