Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

MYSQL json तत्वों को निकालता है जहाँ json होता है

lib_mysqludf_preg उपयोगकर्ता-परिभाषित फ़ंक्शन (UDF) का उपयोग किया गया था:

mysql> DROP TABLE IF EXISTS `orders`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `orders` (
    ->   `id` SERIAL,
    ->   `items` JSON
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `orders`
    ->   (`items`)
    -> VALUES ('
    '>   {
    '>     "10": {
    '>       "name": "item 1",
    '>       "step": "1",
    '>       "price": "140",
    '>       "amount": "4",
    '>       "category": "9"
    '>     },
    '>     "24": {
    '>       "name": "item 2",
    '>       "step": "1",
    '>       "price": "6.2",
    '>       "amount": "1",
    '>       "category": "5"
    '>     },
    '>     "35": {
    '>       "name": "item 3",
    '>       "step": "1",
    '>       "price": "2.9",
    '>       "amount": "3",
    '>       "category": "1"
    '>     },
    '>     "37": {
    '>       "name": "item 4",
    '>       "step": "1",
    '>       "price": "3.9",
    '>       "amount": "2",
    '>       "category": "9"
    '>     }
    '>   }'),
    ->   ('{
    '>     "10": {
    '>       "name": "item 1",
    '>       "step": "1",
    '>       "price": "141",
    '>       "amount": "4",
    '>       "category": "9"
    '>     }
    '>   }'),
    ->   ('{
    '>     "10": {
    '>       "name": "item 1",
    '>       "step": "1",
    '>       "price": "141",
    '>       "amount": "4",
    '>       "category": "8"
    '>     }
    '>   }');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT `id`, `items`
    -> FROM `orders`;
+----+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | items                                                                                                                                                                                                                                                                                                                                                        |
+----+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  1 | {"10": {"name": "item 1", "step": "1", "price": "140", "amount": "4", "category": "9"}, "24": {"name": "item 2", "step": "1", "price": "6.2", "amount": "1", "category": "5"}, "35": {"name": "item 3", "step": "1", "price": "2.9", "amount": "3", "category": "1"}, "37": {"name": "item 4", "step": "1", "price": "3.9", "amount": "2", "category": "9"}} |
|  2 | {"10": {"name": "item 1", "step": "1", "price": "141", "amount": "4", "category": "9"}}                                                                                                                                                                                                                                                                      |
|  3 | {"10": {"name": "item 1", "step": "1", "price": "141", "amount": "4", "category": "8"}}                                                                                                                                                                                                                                                                      |
+----+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.01 sec)

mysql> SET @`category` := '9';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    -> GROUP_CONCAT('
    '>   SELECT
    '>     `id`,
    '>     JSON_EXTRACT(CONCAT(\'[\', REPLACE(`items`, \'"}, "\', \'"}},{"\'), \']\'), ',
    ->       PREG_REPLACE('/^\\[|\\.\\\\\"\\d+\\\\\"\\.category|\\]$/i', '', 
    ->         JSON_SEARCH(
    ->           CONCAT('[', REPLACE(`items`, '"}, "', '"}},{"'), ']'),
    ->           'all',
    ->           @`category`,
    ->           NULL,
    ->           '$**.category'
    ->         )
    ->       ), '
    '>     ) `items`
    '>   FROM `orders`
    '>   WHERE `id` = ', `id`
    ->   SEPARATOR ' UNION ALL ') INTO @`sql`
    -> FROM
    ->   `orders`
    -> WHERE
    ->   JSON_CONTAINS(
    ->     JSON_EXTRACT(`items`,
    ->     '$**.category'),
    ->     CONCAT('"', @`category`, '"')
    ->   );
Query OK, 1 row affected (0.00 sec)

mysql> PREPARE `stmt` FROM @`sql`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE `stmt`;
+----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | items                                                                                                                                                                              |
+----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  1 | [{"10": {"name": "item 1", "step": "1", "price": "140", "amount": "4", "category": "9"}}, {"37": {"name": "item 4", "step": "1", "price": "3.9", "amount": "2", "category": "9"}}] |
|  2 | {"10": {"name": "item 1", "step": "1", "price": "141", "amount": "4", "category": "9"}}                                                                                            |
+----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)

MariaDB में आप बिल्ट-इन फ़ंक्शन का उपयोग कर सकते हैं REGEXP_REPLACE , देखें dbfiddle




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. दो mysql कॉलम जोड़ने और कुल वापस करने में समस्या

  2. पेंटाहो सीई डैशबोर्ड में MySQL क्वेरी पैरामीटर

  3. Laravel 'चालक नहीं ढूँढ सका (SQL:सम्मिलित करें...'

  4. MySQL दुरुपयोग में शामिल हों? यह कितना बुरा हो सकता है?

  5. अंतरराष्ट्रीय और बहुभाषी उद्देश्यों के लिए डेटाबेस मॉडलिंग