ThinkPHP5 MySQL 能把第二张表的数据赋值给第一张表的某个字段吗?

图1是表1,图2是表2.表1中的 advantage 对应表2的 type ,以及图2的 with_id 对应表1的主键ID,也就是说,最终获取出来的数据是这种格式:array(  "id"     =>   1,  "title"    =>   "string",  "content"   =>  "string",  "advantage"   =>  array(6),  "features"   =>  array(4),  "process"   =>  array(7));用join写获取出来的数据就搞不懂该怎么去遍历了。$res = ServiceList::alias("s")    -> field("s.*")    -> join("service_in_arr si", "s.id = si.with_id")    -> field("si.title as title1, si.content as content1")    -> select();这种写出来的数据即图3,表1仅1条数据,表2的数据仅是表1某字段的一个数据集。若我需要按照第一种格式去获取数据,这种该怎么写?请大神莅临指导,非常感谢!


表1获取的格式
<code>
array(  
"id"     =>   1,
"title"    =>   "string",
"content"   =>  "string",
"advantage"   =>  "advantage",
"features"   =>  "features",
"process"   =>  "process"
);
</code>
表2获取的格式
<code>
array(  
"id"     =>   1,
"with_id"  => "string",
"type" => "string",
"title"    =>   "string",
);
</code>
需要的格式
<code>
array(  
"id"     =>   1,
"title"    =>   "string",
"content"   =>  "string",
"advantage"   =>  array(6),
"features"   =>  array(4),
"process"   =>  array(7)
);
</code>

一只斗牛犬
浏览 1697回答 2
2回答

拉风的咖菲猫

如果你实在搞不懂的话,可以先不考虑联接查询,分两次查询,在遍历第一个查询列表中实现数据绑定,先达到目的再进行代码优化为上上选
打开App,查看更多内容
随时随地看视频慕课网APP