THINKPHP多表查询问题
select表有两个字段(stu_id和class_id),怎么写才能够实现,以stu_id的值为条件查询student表,以class_id的值为条件查询class表。...
select表有两个字段(stu_id 和class_id),怎么写才能够实现,以stu_id的值为条件查询student表,以class_id的值为条件查询class表。然后将两个表的值保存到一个数组里面??
我知道大概是用table方法,但是具体不会写。。 展开
我知道大概是用table方法,但是具体不会写。。 展开
1个回答
展开全部
二种方法:
多表查询:
$list=M()->table(array('think_select'=>'this0','think_student'=>'this1','think_class'=>'this2'))
->where('this0.stu_id=this1.id and this0.class_id=this2.id')
->field('this0.id this0_id,this1.id this1_id,this2.id this2_id')->select();
生成sql:
select this0.id this0_id,this1.id this1_id,this2.id this2_id
from think_select this0,think_student this1,think_class this2
where this0.stu_id=this1.id and this0.class_id=this2.id
链接查询:
$list=M()->table("think_select this0")->join('think_student this1 on this0.stu_id=this1.id')
->join('think_class this2 on this0.stu_id=this2.id')
->field('this0.id this0_id,this1.id this1_id,this2.id this2_id')->select();
生成sql:
select this0.id this0_id,this1.id this1_id,this2.id this2_id
from think_select this0 left join think_student this1 on this0.stu_id=this1.id
left join think_class this2 on this0.stu_id=this2.id
多表查询:
$list=M()->table(array('think_select'=>'this0','think_student'=>'this1','think_class'=>'this2'))
->where('this0.stu_id=this1.id and this0.class_id=this2.id')
->field('this0.id this0_id,this1.id this1_id,this2.id this2_id')->select();
生成sql:
select this0.id this0_id,this1.id this1_id,this2.id this2_id
from think_select this0,think_student this1,think_class this2
where this0.stu_id=this1.id and this0.class_id=this2.id
链接查询:
$list=M()->table("think_select this0")->join('think_student this1 on this0.stu_id=this1.id')
->join('think_class this2 on this0.stu_id=this2.id')
->field('this0.id this0_id,this1.id this1_id,this2.id this2_id')->select();
生成sql:
select this0.id this0_id,this1.id this1_id,this2.id this2_id
from think_select this0 left join think_student this1 on this0.stu_id=this1.id
left join think_class this2 on this0.stu_id=this2.id
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
TableDI
2024-07-18 广告
2024-07-18 广告
Excel一键自动匹配,在线免费vlookup工具,3步完成!Excel在线免费vlookup工具,点击93步自动完成vlookup匹配,无需手写公式,免费使用!...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询