Problems with php using mysql query

ThinkPhp query the following content
Game game_zone;
the structure of the table is
zone_name-- Theater name
gid-- Game ID

want to query a returned json in the following format:

"1": 
[
    {"name": "", "id": "1"},
    {"name": "", "id": "2"},
],
"2":
[
    {"name": "", "id": "3"},
    {"name": "", "id": "4"},
],

wonder how to query? There is no way to use dynamic associative preloading and not thinking about subqueries. The most important thing is: the index of the array is the ID, of the game. Or is the data in this format written manually, not queried and converted to json?

the gods who help solve the problem must be rewarded!

Mar.18,2021

consider that it is unlikely that a stored procedure wants to solve with a sql.


query first, and then loop through the results, using id as the index.


fetch the data first, and then array_walk process


should be written manually, query it out and then use php for processing.

public function test()
{
    $list = Db::name('zone_name')->select();
    $res = [];
    foreach ($list as $k => $v) {
        $res[$v['gid']][] = $v;
    }
    return $res;
}

Why do database queries return results that must meet your format requirements?
this in itself is the function that needs to be implemented in the business layer of your code!

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36049-2b529.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36049-2b529.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?