2

I'm trying to get data from a single table using multiple queries. Suppose I have the following table:

MY TABLE:

enter image description here

I'm trying to get PARA_NUMBER 1 and PARA_NUMBER 2 and combining them using this query:

(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:

[
    {
        "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. I'm using very simple PHP code to output the JSON data:

$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);
2
  • You will need to post process in your application framework. What language are you using? Commented Jan 16, 2019 at 7:33
  • I'm using PHP and MySQL. It's just simple getting the results from the table and printing it out in a JSON format. Commented Jan 16, 2019 at 7:41

2 Answers 2

2

If you're using the mysqli interface in PHP, you could do something like this (assuming your connection is called $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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot this worked perfectly! :) I really appreciate your help.
1

Even though the answer of Nick already works, I don't see the need to use UNION. Why don't you use IN instead?

$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);

1 Comment

Wow I didn't knew I can do that. Thanks a lot for this! I really appreciate this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.