MySQL query to search a field with JSON string

前端 未结 2 989
深忆病人
深忆病人 2020-12-30 13:52

This might be simple enough with basic SQL or it might need a REGEXP but I\'ve hit a brick wall.

My data is stored in a JSON string like these two examples (each in

相关标签:
2条回答
  • 2020-12-30 14:35

    In SQL it is really simple, because you only need to identify the last opening bracket. This can you do with substring index.

    Maybe your table called test, and your field with the string called text - this should work

    select text from (select substring_index(text,'[',-1) as text from test) as new where text like '%5%'**
    

    If you have a text between 2 unique delimiters, maybe u find this tutorial helpful

    0 讨论(0)
  • 2020-12-30 14:42

    I think I solved it by using this: AND extra_fields REGEXP '(.*"id":"2".*)("\[.*"1".*\]")'. It's more to do with regular expressions than it is to do with MySQL :P

    comment: (I couldn't find comment button)
    This syntax becomes clearer when you learn that "extra_fields" is the name of the column in the table

    0 讨论(0)
提交回复
热议问题