简体   繁体   中英

JSON to MYSQL with nested queries

Looking for your experience in converting a complicated nested MYSQL query to a JSON file. Below is the code.

SELECT post_id
     , name
     , Email
     , CustomerId
     , DeliveryDate
     , DeliveryTime
     , DeliveryType
     , Zip
     , OrderNote
     , PaymentTotal
     , OrderStatus
  FROM ( SELECT t1.post_id
              , t2.name
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Email
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as CustomerId
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryDate
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryTime
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as DeliveryType
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Zip
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderNote
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as PaymentTotal
              , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as OrderStatus
           FROM table_A t1
         INNER 
           JOIN table_B t2 
             ON FIND_IN_SET(t1.post_id, t2.payment_ids)  
         GROUP 
             BY t1.post_id
              , t2.name  
       ) AS derived_table
 WHERE OrderStatus RLIKE '%trans%|ready'
   AND DeliveryDate >= CURRENT_DATE - INTERVAL 7 DAY
   AND DeliveryType = 'pickup'

Since this has to start with SELECT is there a way to convert this to JSON properly and what would it look like? As I understand it, JSON doesnt like when you start with the SELECT

You can use JSON_OBJECT to create a JSON object out of each row that your query produces. These objects then need to be gathered into an array. Since you're not using MySQL 8+, you will need to simulate the JSON_ARRAYAGG by using GROUP_CONCAT instead. For example (I've left out parts of your query for simplicity):

SELECT CONCAT('[', GROUP_CONCAT(obj), ']') AS JSON
FROM (
    SELECT JSON_OBJECT('post_id', post_id
                     , 'name', name
                     , 'Email', Email
                     , 'CustomerId', CustomerId
                       -- ...
                      ) AS obj
    FROM (SELECT t1.post_id
               , t2.name
               , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as Email
               , MAX(CASE WHEN meta_key = 'value' THEN meta_value ELSE NULL END) as CustomerId
               -- ...
          FROM table_A t1
          INNER JOIN table_B t2 ON FIND_IN_SET(t1.post_id, t2.payment_ids)  
          GROUP BY t1.post_id, t2.name  
    ) AS derived_table
    WHERE OrderStatus RLIKE '%trans%|ready'
      AND DeliveryDate >= CURRENT_DATE - INTERVAL 7 DAY
      AND DeliveryType = 'pickup'
) j

Small demo on dbfiddle

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