APixel Visuals
APixel Visuals

Reputation: 1648

MySQL - SELECT data where JSON key-pair value is something

I'm trying to construct a MySQL query that would do the following:

Sample Data:

---------------------------------------------
|                  colm                     |
---------------------------------------------
|{"level":10, "something": "blah", etc}     |
---------------------------------------------
|{"level":3, "something": "blah", etc}     |
---------------------------------------------
|{"level":7, "something": "blah", etc}     |
---------------------------------------------
|{"level":21, "something": "blah", etc}     |

I need to SELECT rows where colm LIKE "%?%" with the ? being level is less than or equal to (<=) some value. For example, SELECTing all rows where colm has the JSON string's level key-pair value <= 8, getting the middle two rows.

How can I do this? I'm thinking something with JSON functions instead of using LIKE and % wildcards, but I'm not sure exactly what I need to do.

Upvotes: 0

Views: 50

Answers (1)

jake2389
jake2389

Reputation: 1174

Try this:

SELECT * FROM <your-table> WHERE colm->"$.level" <= 8

Upvotes: 1

Related Questions