1

I would like to query data from MySQL database from JSON type column.

I got a query which works and return data from JSON field:

SELECT 
    readouts->"$.temperature_sensor_1.readTimestamp"
 FROM 
    `readouts_store` 
 WHERE 
    `readout_id`=75898

it returns:

"2018-02-04 20:11:53"

But to limit the selected data by date I need it to something like this:

SELECT 
    readouts->"$.temperature_sensor_1.value" 
 FROM 
    `readouts_store` 
 WHERE 
    `application_id`=1 
 AND
    readouts->"$.temperature_sensor_1.readTimestamp", '%Y-%m-%d %H:%i:%s' BETWEEN '2018-02-03' AND '2018-02-05'

It doesn't work so I think it is expected that I convert readouts->"$.temperature_sensor_1.readTimestamp" into DATE type like this:

SELECT 
    readouts->"$.temperature_sensor_1.value" 
 FROM 
    `readouts_store` 
 WHERE 
    `application_id`=1 
 AND
    STR_TO_DATE(readouts->"$.temperature_sensor_1.readTimestamp", '%Y-%m-%d %H:%i:%s') BETWEEN '2018-02-03' AND '2018-02-05'

unfortunately it doesn't work because STR_TO_DATE() returns NULL:

SELECT 
    STR_TO_DATE(readouts->"$.temperature_sensor_1.readTimestamp", '%Y-%m-%d %H:%i:%s')
FROM
    readouts_store
WHERE 
   `readout_id`=75898

Instead of STR_TO_DATE() I also tried DATE() and CONVERT("", DATE) but the result is the same.

Is such functionality not supported in MySQL? I cannot find any reference in documentation covering this.

1 Answer 1

3

When you use -> the return value is itself a JSON value, so it appears to have double-quotes.

Use ->> to return a "raw" scalar value with the quotes removed.

See https://dev.mysql.com/doc/refman/5.7/en/json.html for more information on the difference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.