MySQL: index json arrays of variable length?

后端 未结 3 1048
轻奢々
轻奢々 2021-02-04 06:58

I want to make a tags column of type json:

e.g.,

id  |  tags
=========================================
1   |  \'["tag1"         


        
3条回答
  •  孤街浪徒
    2021-02-04 07:50

    It's now possible with MySQL 8.0.17+

    Something like this (not tested)

    CREATE TABLE posts (
        id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        tags JSON,
        INDEX tags( (CAST(tags AS VARCHAR(32) ARRAY)) )
        );
    

    Use it this way:

    SELECT * FROM posts 
             WHERE JSON_CONTAINS(tags, CAST('[tag1, tag2]' AS JSON));
    

    More details and samples here: https://dev.mysql.com/doc/refman/8.0/en/json.html

提交回复
热议问题