zoukankan      html  css  js  c++  java
  • MySQL查询字段类型为json的数据

    测试表如下:

    /*
     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
    
  • 相关阅读:
    【读书笔记】房龙-人类的故事
    【读书笔记】曼昆-经济学原理:宏观经济学分册
    【读书笔记】曼昆-经济学原理:微观经济学分册
    ES6数组去重
    switch语句
    if语句的优化
    程序的三大结构
    NaN
    js中常见的数据类型
    变量的命名规则
  • 原文地址:https://www.cnblogs.com/sirxy/p/11213584.html
Copyright © 2011-2022 走看看