• KingbaseES json操作符


    下表列出了常用的json数据类型操作符:

    操作符操作符右侧数据类型返回类型描述
    ->intjson or jsonb获得 JSON 数组元素(索引从 0 开始,负整数从末尾开始计)
    ->textjson or jsonb通过键获得 JSON 对象域
    ->>inttext以text形式获得 JSON 数组元素
    ->>texttext以text形式获得 JSON 对象域
    #>text[]json or jsonb获取在指定路径的 JSON 对象
    #>>text[]text以text形式获取在指定路径的 JSON 对象
    构建测试数据
    1. CREATE TABLE student (stu_id serial NOT NULL PRIMARY KEY,stu_info json NOT NULL);
    2. INSERT INTO student (stu_info)
    3. VALUES
    4. (
    5. '{
    6. "name": "lisi",
    7. "information":
    8. {
    9. "mobile_number": "13700000001",
    10. "branch": "Computer",
    11. "rank":12
    12. }
    13. }'
    14. ),
    15. (
    16. '{
    17. "name": "zhangsan",
    18. "information":
    19. {
    20. "mobile_number": "13700000002",
    21. "branch": "Computer",
    22. "rank":1
    23. }
    24. }'
    25. ),
    26. (
    27. '{
    28. "name": "zhouxinxin",
    29. "information":
    30. {
    31. "mobile_number": "13700000003",
    32. "branch": "Car",
    33. "rank":2
    34. }
    35. }'
    36. ),
    37. (
    38. '{
    39. "name": "lilei",
    40. "information":
    41. {
    42. "mobile_number": "13700000004",
    43. "branch": "Civil",
    44. "rank":6
    45. }
    46. }'
    47. );
    48. INSERT INTO student (stu_info) --数组类型的json
    49. VALUES
    50. (
    51. '[{
    52. "name": "wanwu"},
    53. {"information":
    54. {
    55. "mobile_number": "13700000005",
    56. "branch": "Computer",
    57. "rank":11
    58. }
    59. }]'
    60. )

    1.使用索引来获取学生名字(返回的json类型的数据)

    1. test=# SELECT stu_info ->0 AS StudentName FROM student;
    2. studentname
    3. ------------------
    4. { +
    5. "name": "wanwu"}
    6. (5 行记录)
    7. test=# SELECT stu_info ->0 ->'name' AS StudentName FROM student;
    8. studentname
    9. -------------
    10. "wanwu"
    11. (5 行记录)

    2.使用json键来获取学生名字(返回的json类型的数据)

    1. test=# SELECT stu_info -> 'name' AS StudentName FROM student;
    2. studentname
    3. --------------
    4. "lisi"
    5. "zhangsan"
    6. "zhouxinxin"
    7. "lilei"
    8. (5 行记录)

    3.使用json键来获取学生名字(返回的字符串类型的数据)

    1. test=# SELECT stu_info ->> 'name' AS StudentName FROM student;
    2. studentname
    3. -------------
    4. lisi
    5. zhangsan
    6. zhouxinxin
    7. lilei
    8. (5 行记录)

    4.获取学生的手机号码

    1. test=# SELECT stu_info #>> '{information,mobile_number}' AS phone FROM student;
    2. phone
    3. -------------
    4. 13700000001
    5. 13700000002
    6. 13700000003
    7. 13700000004
    8. (5 行记录)

    5.在where条件中使用json操作符

    1. test=# SELECT stu_info ->> 'name' AS StudentName FROM student WHERE stu_info -> 'information' ->> 'branch' = 'Computer';
    2. studentname
    3. -------------
    4. lisi
    5. zhangsan
    6. (2 行记录)
    7. test=# SELECT stu_info ->> 'name' AS StudentName FROM student WHERE stu_info #>> '{information,branch}' = 'Computer';
    8. studentname
    9. -------------
    10. lisi
    11. zhangsan
    12. (2 行记录)

    ‘>>’ 操作符返回的是文本类型的数据,‘>’ 操作符返回的是json/jsonb类型的数据。
    操作符右侧使用数字则只对数组类型的json数据有效果,反之亦然。

  • 相关阅读:
    电商评论文本挖掘
    MyBatis入门(二)MyBatis增删改查
    Dataset 的一些 Java api 操作
    解密Prompt系列2. 冻结Prompt微调LM: T5 & PET & LM-BFF
    数据结构与算法:堆排序和TOP-K问题
    go实现命令行的工具cli
    粮食淘洗机结构设计(说明书+任务书+开题+文献综述+翻译及原文+cad图纸)
    XSS进阶三
    百度千帆大模型文心一言api调用
    苞米豆的多数据源 → dynamic-datasource-spring-boot-starter,挺香的!
  • 原文地址:https://blog.csdn.net/lyu1026/article/details/126900055