mysql将json_objects转换为数组

时间:2017-07-05 11:40:13

标签: mysql mysqli

我有这个查询

SELECT json_object('image_name', image_name, 'iid', iid) FROM `posts_images` WHERE `pid` = '62'

导致此

Image

我可以将这些结果转换为同一查询中的一行json数组吗?

3 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

$query = "SELECT json_object('image_name', image_name, 'iid', iid) FROM `posts_images` WHERE `pid` = '62'";
$result = mysqli_query($query);
$data = array();
while($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}
print json_encode($data);

答案 1 :(得分:0)

var o = {"0":"1","1":"2","2":"3","3":"4"};

var arr = $.map(o, function(el) { return el; })

console.log(arr)

试试这个

答案 2 :(得分:0)

我想通了

SELECT CONCAT(
    '[',  
            GROUP_CONCAT(json_object('image_name', image_name, 'iid', iid)),
    ']'
) 
FROM posts_images WHERE pid = '62'
  

[{" iid":99," image_name":" 6_1499203101812.jpg"},{" iid":98,   " image_name":" 6_1499203101708.jpg"},{" iid":97," image_name":   " 6_1499203101651.jpg"},{" iid":96," image_name":   " 6_1499203101574.jpg"},{" iid":95," image_name":   " 6_1499203101472.jpg"},{" iid":100," image_name":   " 6_1499203101886.jpg"},{" iid":101," image_name":   " 6_1499203101960.jpg"},{" iid":102," image_name":   " 6_1499203102020.jpg"}]