• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

在选择查询中选择MYSQL

php 来源:Root Haxx 4次浏览

您好!我有一个需要输出屏幕数据的查询。
每个屏幕有4个展位可供展示。我需要一个查询有一个结果(因为我只有一个屏幕),并有4个展位(怎么我分配的4个展位)在选择查询中选择MYSQL

这里是我的查询:

$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" 
} 
] 

===========解决方案如下:

结果还好..只是需要一点点迭代来获取值..

$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] 

数组键不能有相同的键名


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)