dongqiang8058
dongqiang8058
采纳率100%
2018-08-17 06:02

求助:无法从MySQL找到JSON_CONTAINS!

已采纳

在MySQL中,我使用PHP的json_encode将数据存储在“jsonValue”字段中:

{"product_category":[[{"category":["28"]},{"product":["16","22","64"]}]]}

通过PHP,我希望通过编写以下查询来获取数据:

SELECT * FROM `dbTable` 
WHERE JSON_CONTAINS(jsonValues, '"category":["28"]');

但是,它还是报错:

3141 - Invalid JSON text in argument 2 to function json_contains: "The document root must not follow by other values." at position 10.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • drrkgbm6851 drrkgbm6851 3年前

    It returns the row after writing the below codes:

    SELECT * FROM `dbTable` WHERE JSON_CONTAINS(jsonValues, '{"category":["28"]}', '$.product_category');
    
    点赞 评论 复制链接分享
  • duanduanxi9441 duanduanxi9441 3年前

    If None of the above method works for you then try below code it should work.

    SELECT * FROMdbTable WHERE JSON_CONTAINS(jsonValues, '{"product_category": [[{"category": ["28"]}]]}');

    点赞 1 评论 复制链接分享
  • douji0588 douji0588 3年前

    Have you tried adding the curly brackets around your search term, as the mysql doc does?

    SELECT * FROM `dbTable` WHERE JSON_CONTAINS(jsonValues, '{"category":["28"]}');
    
    点赞 评论 复制链接分享

相关推荐