Last active
May 11, 2017 08:33
-
-
Save lvsmart/967cc391817f48fcb71be50fc4b42ad7 to your computer and use it in GitHub Desktop.
mysql json query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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