karmendra
karmendra

Reputation: 2243

MySql JSON query between a range

I have a table card_percentage, with DDL:

CREATE TABLE card_percentage
(
    id   int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    jdoc json NULL
);

It has one row of data

id | jdoc
------------ 
 1 | {"AMXC": [{"enabled": "y", "bank_code": "AMXC", "max_amount": 99999999.99, "min_amount": 0.0, "percentage": 2.5}], "VISA": [{"enabled": "y", "bank_code": "VISA", "max_amount": 1999.99, "min_amount": 0.0, "percentage": 2.8}, {"enabled": "y", "bank_code": "VISA", "max_amount": 99999999.99, "min_amount": 2000.0, "percentage": 1.8}]}

Beautified JSON for readability

{
  "AMXC": [
    {
      "enabled": "y",
      "bank_code": "AMXC",
      "min_amount": 0.0,
      "max_amount": 99999999.99,
      "percentage": 2.5
    }
  ],
  "VISA": [
    {
      "enabled": "y",
      "bank_code": "VISA",
      "min_amount": 0.0,
      "max_amount": 1999.99,
      "percentage": 2.8
    },
    {
      "enabled": "y",
      "bank_code": "VISA",
      "min_amount": 2000.0,
      "max_amount": 99999999.99,
      "percentage": 1.8
    }
  ]
}

Now I want to select percentage for VISA when amount 200 is between min_amount and max_amount. For this I am using following query but it is giving nothing in result set.

SELECT id,
       jdoc -> '$.VISA[*].percentage' percentage
FROM card_percentage
WHERE id = 1
  AND jdoc -> '$.VISA[*].min_amount' <= 200
  AND jdoc -> '$.VISA[*].max_amount' >= 200
;

Upvotes: 0

Views: 907

Answers (1)

karmendra
karmendra

Reputation: 2243

Ok based on @Akina's comment about using JSON_TABLE, I did further research.

Following is my query, which works really well.

SELECT id, jdoc.percentage
FROM card_percentage, JSON_TABLE(
             jdoc,
             '$.VISA[*]'
             COLUMNS (
                 rowid FOR ORDINALITY,
                 enabled VARCHAR(1) PATH '$.enabled' DEFAULT '"n"' ON EMPTY DEFAULT '"n"' ON ERROR,
                 bank_code VARCHAR(4) PATH '$.bank_code' DEFAULT '"NCOD"' ON EMPTY ,
                 min_amount INT PATH '$.min_amount' DEFAULT '0' ON EMPTY ,
                 max_amount INT PATH '$.max_amount' DEFAULT '0' ON EMPTY ,
                 percentage DECIMAL(10, 2) PATH '$.percentage' DEFAULT '0' ON EMPTY DEFAULT '0' ON ERROR
                 )
         ) AS jdoc
WHERE jdoc.min_amount <= 200
  AND jdoc.max_amount >= 200
;

To get this working, this article "JSON_TABLE – The Best of Both Worlds" really helped.

Upvotes: 2

Related Questions