查询MySQL表以返回JSON

时间:2018-04-02 14:30:26

标签: php mysql json

我有一个带有温度传感器数据的MySQL表,我正在尝试将它的数据转换为JSON数组,以便输出到带有JavaScript图表的PHP页面。

这是我的表和一些示例数据:

+----+----------+---------------------+-------+
| id | sensorId |    dateRecorded     | tempF |
+----+----------+---------------------+-------+
|  1 |        1 | 2018-03-31 10:00:00 |  34.2 |
|  2 |        2 | 2018-03-31 10:00:00 |  83.1 |
|  3 |        2 | 2018-03-31 10:05:00 |  44.5 |
|  4 |        1 | 2018-03-31 10:05:00 |  65.2 |
+----+----------+---------------------+-------+


我想要输出的JSON结构是这样的:

data: [
    { dateRecorded: '2018-03-31 10:00:00', sensor1: 34.2, sensor2: 83.1 },
    { dateRecorded: '2018-03-31 10:05:00', sensor1: 65.2, sensor2: 44.5 }
]


我可以单独返回每个传感器数据,但我似乎无法弄清楚如何通过dateRecorded列对输出进行分组,然后将每个传感器的数据放在一行中自己的键/值对中?

1 个答案:

答案 0 :(得分:2)

你JSON输出结构是无效的JSON。

data: [
    { dateRecorded: '2018-03-31 10:00:00', sensor1: 34.2, sensor2: 83.1 },
    { dateRecorded: '2018-03-31 10:05:00', sensor1: 65.2, sensor2: 44.5 }
]

这是一个有效的JSON结构,所以这是我在我的回答中的目标。

{
    "data": [{
        "dateRecorded": "2018-03-31 10:00:00",
        "sensor1": 34.2,
        "sensor2": 83.1
    }, {
        "dateRecorded": "2018-03-31 10:05:00",
        "sensor1": 65.2,
        "sensor2": 44.5
    }]
}

这仅适用于纯MySQL。

创建表/插入数据

CREATE TABLE Table1
    (`id` int, `sensorId` int, `dateRecorded`datetime, `tempF` double)
;

INSERT INTO Table1
    (`id`, `sensorId`, `dateRecorded`, `tempF`)
VALUES
    (1, 1, '2018-03-31 10:00:00', 34.2),
    (2, 2, '2018-03-31 10:00:00', 83.1),
    (3, 2, '2018-03-31 10:05:00', 44.5),
    (4, 1, '2018-03-31 10:05:00', 65.2)
;

使用MySQL生成内部JSON结构,如

{
        "dateRecorded": "2018-03-31 10:00:00",
        "sensor1": 34.2,
        "sensor2": 83.1
    }, {
        "dateRecorded": "2018-03-31 10:05:00",
        "sensor1": 65.2,
        "sensor2": 44.5
    }

您需要使用函数CONCAT和GROUP_CONCAT来生成JSON字符串。

<强>查询

SELECT 
   CONCAT (
       '{'
     , '"dateRecorded": ', '"', Table1.dateRecorded, '"'
     , ','
     , GROUP_CONCAT(     
         CONCAT(
           '"sensor', Table1.sensorId, '":', Table1.tempF
         )
         ORDER BY
          Table1.id ASC
       )
     , '}'
   )
    AS json_data_records
FROM 
 Table1 
GROUP BY
 Table1.dateRecorded
ORDER BY 
 Table1.dateRecorded ASC

<强>结果

|                                                     json_data_records |
|-----------------------------------------------------------------------|
| {"dateRecorded": "2018-03-31 10:00:00","sensor1":34.2,"sensor2":83.1} |
| {"dateRecorded": "2018-03-31 10:05:00","sensor2":44.5,"sensor1":65.2} |

参见演示http://www.sqlfiddle.com/#!9/d6db452/6

p.s第二条记录中传感器1和传感器2的顺序相反。
因为在GROUP_CONCAT函数中关闭了ORDER BY id ASC。
旁边此订单存在于源数据中。

生成完整的JSON,如

{
    "data": [{
        "dateRecorded": "2018-03-31 10:00:00",
        "sensor1": 34.2,
        "sensor2": 83.1
    }, {
        "dateRecorded": "2018-03-31 10:05:00",
        "sensor1": 65.2,
        "sensor2": 44.5
    }]
}

我们需要更改现有查询,以便使用CONCAT和GROUP_CONCAT合并最后一个输出。

<强>查询

SELECT 
 CONCAT(
    '{'
      , '"data": ['
          , GROUP_CONCAT(json_records.json)
     , ']'
   , '}'
 )
  AS json
FROM ( 
  SELECT
     CONCAT (
         '{'
       , '"dateRecorded": ', '"', Table1.dateRecorded, '"'
       , ','
       , GROUP_CONCAT(     
           CONCAT(
             '"sensor', Table1.sensorId, '":', Table1.tempF
           )
           ORDER BY
            Table1.id ASC
         )
       , '}'
     ) 
      AS json
  FROM 
   Table1 
  GROUP BY
   Table1.dateRecorded
  ORDER BY 
   Table1.dateRecorded ASC
) 
 AS json_records

<强>结果

|                                                                                                                                                    json |
|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| {"data": [{"dateRecorded": "2018-03-31 10:00:00","sensor1":34.2,"sensor2":83.1},{"dateRecorded": "2018-03-31 10:05:00","sensor2":44.5,"sensor1":65.2}]} |

参见演示http://www.sqlfiddle.com/#!9/d6db452/19