简体   繁体   中英

Search on Json with patterns (MYSQL Regex)

i having problems with a proyect... it's like a experiment or idk... It's a Relational database with Json on it like a response of a form

It's like

Select details from mytable where id = 1

and i get something like this

{
   "altitud":"1253,m.s.n.m",
   "tipo_vacacion":[
      "1"
   ],
   "ubicacion":{
      "scalar":"1"
   },
   "area":{
      "scalar":"800"
   },
   "huespedes":{
      "scalar":"4"
   },
   "acompanamiento":{
      "scalar":"1"
   },
   "departamento":{
      "scalar":"26"
   },
   "municipio":{
      "scalar":"Socorro"
   },
   "direccion":{
      "scalar":"Carrera....Calle"
   },
   "encargado":{
      "scalar":"Hernando Marquez"
   },
   "encargado_telf":{
      "scalar":"3158895585"
   },
   "traslado_km":{
      "scalar":"0"
   },
   "traslado_tiempo":{
      "scalar":""
   },
   "acceso_inmueble":{
      "0":"0"
   },
   "clima":{
      "scalar":"1"
   },
   "servicios":{
      "0":"0",
      "1":"1",
      "2":"2",
      "3":"4",
      "4":"7",
      "5":"8"
   },
   "actividades":{
      "scalar":"Cocina, Hamacas, Caminatas por el jard\u00edn, Retiros de Silencio."
   },
   "cultura":{
      "scalar":"Museo Casa de la Cultura del Socorro.\r\nTeatro Municipal, Catedral."
   },
   "ecodestinos":{
      "0":"1",
      "1":"10",
      "2":"11"
   },
   "cobertura_celular":{
      "0":"0",
      "1":"1",
      "2":"2",
      "3":"3"
   },
   "seguridad":{
      "0":"41",
      "1":"41",
      "2":"41",
      "3":"41",
      "4":"41"
   },
   "matricula":{
      "scalar":"000000000000"
   },
   "una_semana":{
      "scalar":"0"
   },
   "medio_mes":{
      "scalar":"1"
   },
   "recicla":{
      "0":"0",
      "1":"1",
      "2":"2",
      "3":"3"
   },
   "energia_limpia":{
      "scalar":"en Complementaci\u00f3n."
   },
   "manejo_aguas":{
      "scalar":"En an\u00e1lisis."
   }
}

They want me to create a "like" query to know something like

Select id from mytable where details like '"servicios":%"1":"1"%"2":"2"%"3":"4"%"4":"7"'

(That query its not working by the way...)

It looks simple at first but, do you notice that "scalar" thing? and sometimes they say to me: That element it's not on the value, its on the key, but we are not sure where is it normally so, you need to show us both

Thats why i come here, cuz i spend 1 week trying (i solved that problem getting all the table and creating a function on php but now they want it with mysql...)

Please, help me

Try this:

SELECT * FROM `mytable` 
WHERE  `details`  REGEXP  
'"servicios":\{[ [.newline.]]*"0":"0",[ [.newline.]]*"1":"1",[ [.newline.]]*"2":"2",[ [.newline.]]*"3":"4",[ [.newline.]]*"4":"7",[ [.newline.]]*"5":"8"[ [.newline.]]*\}'

http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM