V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
nidalee
V2EX  ›  MySQL

Mysql 8.0 JSON 数据类型的数组如何完全匹配

  •  
  •   nidalee · 2023-01-10 17:39:15 +08:00 · 961 次点击
    这是一个创建于 476 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如数据库有一个 test 表,有 3 条数据

    id num_array
    1 [1,2,3]
    2 [1,2]
    3 [1]

    我想搜索到只包含[1,2]的数据,但是目前看了下网上的一些资料,都没有这种纯数组操作方法的说明

    SELECT * FROM test WHERE JSON_CONTAINS (num_array,'[1,2]')

    返回的数据是

    id num_array
    1 [1,2,3]
    2 [1,2]

    有什么办法执行上面的 sql 可以做到只返回

    id num_array
    2 [1,2]
    nidalee
        1
    nidalee  
    OP
       2023-01-10 18:03:12 +08:00
    家人们,我悟了
    SELECT * FROM requirement_district WHERE district -> '$[*]' = JSON_ARRAY(1,2)
    这么写就行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1390 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:59 · PVG 00:59 · LAX 09:59 · JFK 12:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.