Extract data from json inside mysql field

前端 未结 5 1350
无人共我
无人共我 2021-01-04 20:47

I\'ve got a a table with rows, and one of the rows has a field with data like this

{\"name\":\"Richard\",\"lastname\":null,\"city\":\"Olavarria\",\"cityId\":         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 21:27

    MySQL has got support for JSON in version 5.7.7 http://mysqlserverteam.com/json-labs-release-native-json-data-type-and-binary-format/ You will be able to use the jsn_extract function to efficiently parse your JSON string.

    If you have an older version and you want to solve it purely in mysql then I am afraid you have to treat it as a string and cut the value out of it (just normal string functions or use regular expressions) This is not elegant but it will work

    http://sqlfiddle.com/#!9/97cfd/14

    SELECT
      DISTINCT(substring(jsonfield, locate('"city":',jsonfield)+8,
         locate('","', jsonfield, locate('"city":',jsonfield))-locate('"city":',jsonfield)-8)
      )
    FROM
      ForgeRock
    

提交回复
热议问题