测试表如下:
/* Navicat Premium Data Transfer Source Server : Source Server Type : MySQL Source Server Version : 50717 Source Host : localhost:3306 Source Schema : ceshi Target Server Type : MySQL Target Server Version : 50717 File Encoding : 65001 Date: 19/07/2019 15:08:52*/SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for infojson-- ----------------------------DROP TABLE IF EXISTS `infojson`;CREATE TABLE `infojson` ( `id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `info` json NULL, PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;-- ------------------------------ Records of infojson-- ----------------------------INSERT INTO `infojson` VALUES ('1', '{\"sex\": \"male\", \"username\": \"wangermazi\"}');SET FOREIGN_KEY_CHECKS = 1;
- 获取infojson表中username的值,查询方法有:
select infojson.id, json_extract(infojson.info, '$.username') as username from infojson;
结果如下:
id username 1 "wangermazi"
- 以上这条sql查出的username的值是带有双引号的,但是有时我们不需要双引号,此时就需要用到下面这种方式:
select infojson.id, infojson.info ->> '$.username' as username from infojson;
结果如下:
id username 1 wangermazi