简体   繁体   English

从单个表获取数据到多个json数组

[英]Getting data from a single table into multiple json arrays

I'm trying to get data from a single table using multiple queries. 我正在尝试使用多个查询从单个表中获取数据。 Suppose I have the following table: 假设我有下表:

MY TABLE: 我的桌子:

在此处输入图片说明

I'm trying to get PARA_NUMBER 1 and PARA_NUMBER 2 and combining them using this query: 我正在尝试获取PARA_NUMBER 1和PARA_NUMBER 2并使用以下查询它们组合

(SELECT * FROM `t_sen` WHERE `PARA_NUMBER` = 1) 
UNION 
(SELECT * FROM `t_sen` WHERE `PARA_NUMBER` = 2)

And then I get the results in JSON format which looks something like this: 然后,我得到JSON格式的结果,如下所示:

[
    {
        "ID": "1",
        "PARA_NUMBER": "1",
        "TEXT": "Hello"
    },
    {
        "ID": "2",
        "PARA_NUMBER": "1",
        "TEXT": "how"
    },
    // rest of the rows

    {
        "ID": "6",
        "PARA_NUMBER": "2",
        "TEXT": "Hope"
    },
    {
        "ID": "7",
        "PARA_NUMBER": "2",
        "TEXT": "you're"
    },
    // rest of the rows
]

However, I want something like this: 但是,我想要这样的东西:

[
    "1": {
            "ID": "1",
            "PARA_NUMBER": "1",
            "TEXT": "Hello"
         },
         {
            "ID": "2",
            "PARA_NUMBER": "1",
            "TEXT": "how"
         },
         // rest of the rows

    "2": {
            "ID": "6",
            "PARA_NUMBER": "2",
            "TEXT": "Hope"
         },
         {
            "ID": "7",
            "PARA_NUMBER": "2",
            "TEXT": "you're"
         },
         // rest of the rows
]

Where every PARA_NUMBER have their own branches. 每个PARA_NUMBER都有自己的分支。 I'm using very simple PHP code to output the JSON data: 我正在使用非常简单的PHP代码来输出JSON数据:

$myquery = "
        (SELECT * FROM t_sen WHERE PARA_NUMBER = 1)
        UNION
        (SELECT * FROM t_sen WHERE PARA_NUMBER = 2)
    ";

$query = mysqli_query($conn, $myquery);

    if (!$query) {
        echo mysqli_error($conn);  //You need to put $conn here to display error.
        die();
    }

    $data = array();

    while($row = mysqli_fetch_assoc($query)) {
        $data[] = $row;
    }

    echo json_encode($data, JSON_PRETTY_PRINT);

If you're using the mysqli interface in PHP, you could do something like this (assuming your connection is called $conn ): 如果您在PHP中使用mysqli接口,则可以执行以下操作(假设您的连接称为$conn ):

$result = $conn->query('(SELECT * FROM `t_sen` WHERE `PARA_NUMBER` = 1) 
UNION 
(SELECT * FROM `t_sen` WHERE `PARA_NUMBER` = 2)');
$out = array();
while ($row = $result->fetch_assoc()) {
    $out[$row['PARA_NUMBER']][] = $row;
}
echo json_encode($out);

Even though the answer of Nick already works, I don't see the need to use UNION . 即使Nick的答案已经奏效,但我认为不需要使用UNION Why don't you use IN instead? 您为什么不改用IN

$result = $conn->query('SELECT * FROM `t_sen` WHERE `PARA_NUMBER` IN (1,2)');
$out = array();
while ($row = $result->fetch_assoc()) {
    $out[$row['PARA_NUMBER']][] = $row;
}
echo json_encode($out);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM