Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Select查询中选择MYSQL_Php_Mysql_Laravel - Fatal编程技术网

Php 在Select查询中选择MYSQL

Php 在Select查询中选择MYSQL,php,mysql,laravel,Php,Mysql,Laravel,你好我有一个需要输出屏幕数据的查询。 每个屏幕有4个展台要显示。我需要一个带有一个结果的查询(因为我只有一个屏幕),其中有4个隔间(因为我分配了4个隔间) 这是我的疑问: $result = DB::select(" SELECT `A`.`scr_name`, `A`.`mission_id`, `B`.`booth_id`, `E`.`name` FROM `tbl_screen` `A` LEFT JO

你好我有一个需要输出屏幕数据的查询。
每个屏幕有4个展台要显示。我需要一个带有一个结果的查询(因为我只有一个屏幕),其中有4个隔间(因为我分配了4个隔间)

这是我的疑问:

$result = DB::select("
    SELECT
        `A`.`scr_name`,
        `A`.`mission_id`,
        `B`.`booth_id`,
        `E`.`name`
    FROM `tbl_screen` `A`
    LEFT JOIN
        `tbl_screen_booths` `B`
    ON `B`.`screen_id` = `A`.`id`
    LEFT JOIN
        `tbl_service_booths` `C`
    ON `B`.`booth_id` = `C`.`id`
    LEFT JOIN
        `tbl_missions_services` `D`
    ON `C`.`mission_services_id` = `D`.`id`
    LEFT JOIN
        `tbl_services` `E`
    ON `D`.`service_id` = `E`.`id`
    WHERE `A`.`mission_id` = $mission_id
    GROUP BY
        `B`.`booth_id`;
");

return $result;   
我想要这样的东西:

"scr_name": "Test Screen",
"mission_id": 2,
"name": "booth1", //index[0]
"name": "booth2", //index[1]
"name": "booth3", //index[2]
"name": "booth4" //index[4]
[
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 7,
"name": "booth1"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 9,
"name": "booth2"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 10,
"name": "booth3"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 11,
"name": "booth4"
}
]
我的查询返回如下内容:

"scr_name": "Test Screen",
"mission_id": 2,
"name": "booth1", //index[0]
"name": "booth2", //index[1]
"name": "booth3", //index[2]
"name": "booth4" //index[4]
[
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 7,
"name": "booth1"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 9,
"name": "booth2"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 10,
"name": "booth3"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 11,
"name": "booth4"
}
]

结果还可以。。只需要一点迭代就可以得到值

$data['scr_name'] = $result[0]->scr_name;
$data['mission_id'] = $result[0]-> mission_id;

foreach($result as $index => $item) {
     $data['name'.($index+1)] => $item->name;
}
结果:

"scr_name": "Test Screen",
"mission_id": 2,
"name1": "booth1", //index[0]
"name2": "booth2", //index[1]
"name3": "booth3", //index[2]
"name4": "booth4" //index[4]

数组键不能有相同的键名

,但是如果我有多个屏幕,这不合适,因为它将返回多个结果。然后需要迭代数据。。放点逻辑。。然后生产出产品。。这只是您数据中的样本