Get Json keys in a MySQL for a particular value

前端 未结 2 1019
粉色の甜心
粉色の甜心 2021-01-28 05:32

I just installed MySQL 5.7.27 and I would like to use some Json fields so, I created some records, for example this value in a field:

{
  \"Intitule\": {
    \"n         


        
2条回答
  •  一个人的身影
    2021-01-28 06:14

    This answer is to expand on the comment I made above.

    When you store data in non-relational format, you should understand that the structure of the data must serve the queries you need to run. You can't just store any data and expect it to be easy to query.

    For example, if you need to search for the subdocument where is_filter is true, then store your JSON like this:

    {
      "filter": {
        "label": "Intitule",
        "name": "Intitule de la formation",
        "stats": false,
        "is_array": false,
        "chart": "pie",
        "col": "6"
      },
      "not_filter": {
        "label": "Fin",
        "name": "Date de fin",
        "stats": false,
        "is_array": false,
        "chart": "pie",
        "col": "6"
      }
    }
    

    If you need to use a variety of queries against the data, then using non-relational data like JSON is probably not as useful as storing the data in a relational manner.

    mysql> select * from mytable;
    +----------+--------------------------+-------+----------+-----------+-------+------+
    | label    | name                     | stats | is_array | is_filter | chart | col  |
    +----------+--------------------------+-------+----------+-----------+-------+------+
    | Intitule | Intitule de la formation |     0 |        0 |         1 | pie   |    6 |
    | Fin      | Date de fin              |     0 |        0 |         0 | pie   |    6 |
    +----------+--------------------------+-------+----------+-----------+-------+------+
    

    Then you can query by any attribute:

    SELECT label FROM mytable WHERE is_filter = true;
    

    NoSQL is more flexible than SQL for storing data.

    SQL is more flexible than NoSQL for querying data.

提交回复
热议问题