简体   繁体   中英

SQL - SELECT WHERE date stored in a JSON varchar is greater or less than a fixed date

I have a VARCHAR column where user store custom data from Input, Select, Checkbox and Input type date.

[{"i":24,"v":[0,1,4]},{"i":26,"v":[2,3,6,10]},{"i":41,"v":"2019-03-18"},{"i":27,"v":[26]},{"i":6,"v":"Very happy to be here."},{"i":13,"v":4},{"i":22,"v":9},{"i":3,"v":"Numerous"},{"i":4,"v":3},{"i":29,"v":[2,3]},{"i":30,"v":[3,4]}]

I know ID 41 is a date. I want to SELECT this user if ID 41 exists and if the data is greater than a fixed date.

My idea is to use REGEXP to test if ID 41 exists : REGEXP \\'(\\{"i":41,) and then to catch the date and test it (TO_DATE ?). How can I do this second part ?

Edit - I have found a solution to catch the date with REGEXP_SUBSTR :

SELECT user_id, REGEXP_SUBSTR(my_json_col, \'(?<=\{"i":41,"v":")(.*?)(?="\})\') AS dt FROM ...

=> dt : 2019-03-18

I still cannot test the value.

You should extract the data in separate columns at insert time, for performance reasons. The way you want to do it is VERY slow.

However, if you MUST do it that way, check this out: How can write queries in MySQL that can parse JSON data in a column?

It shows you how to address the data directly, not with regular expressions. AFAIK this works for MySQL/Postgres, most RDBMS have JSON capabilities but I don't think it is very standardized, so check out the appropriate dox.

I think JSON_EXTRACT(column,'$[41].v') is what you are after, check https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html for more insight.

Solution with REGEXP_SUBSTR and HAVING :

SELECT
    user_id,
    REGEXP_SUBSTR(my_json_col, \'(?<=\{"i":41,"v":")(.*?)(?="\})\') AS dt
FROM mytable
HAVING
    DATE(dt) < DATE(NOW())
    and DATE(dt) > DATE("2019-03-15")
    and dt <> ""

It works well.

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