Skip to content

Instantly share code, notes, and snippets.

@lvsmart
Last active May 11, 2017 08:33
Show Gist options
  • Select an option

  • Save lvsmart/967cc391817f48fcb71be50fc4b42ad7 to your computer and use it in GitHub Desktop.

Select an option

Save lvsmart/967cc391817f48fcb71be50fc4b42ad7 to your computer and use it in GitHub Desktop.
mysql json query
def condition_sql(path, search_params):
from_condition = []
if not search_params:
return ''
if type(search_params) == list:
for i in search_params:
from_condition.append('JSON_CONTAINS(param_json, \'%s\', "%s")' % (json.dumps(i), path))
elif type(search_params) == dict:
for k, v in search_params.items():
if v:
from_condition.append(condition_sql('%s.%s' % (path, k), v))
elif type(search_params) == str:
from_condition.append('JSON_EXTRACT(param_json, "%s") = "%s"' % (path, search_params))
else:
from_condition.append('JSON_EXTRACT(param_json, "%s") = %s' % (path, search_params))
return ' AND '.join(from_condition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment