think php 子查询,使用thinkPHP怎么实现一个子查询语句
使用thinkPHP怎么實現一個子查詢語句
發布時間:2021-01-30 13:31:08
來源:億速云
閱讀:85
作者:Leah
這篇文章給大家介紹使用thinkPHP怎么實現一個子查詢語句,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
理解好sql語句,就能用好thinkphp等框架中的數據庫操作
原sql:SELECT?a.*,b.*?from?(SELECT?a.id?as?opener_id,a.name,sum(c.money)?as?bonus_money,c.year,c.month?from?sh_opener?a
LEFT?JOIN?sh_opener_bonus?b?on?a.id?=?b.opener_id
LEFT?JOIN?sh_incentive?c?on?b.incentive_id?=?c.id
where?a.agent_id?=?3?and?a.status?=?1?and?c.year?=?2015?and?c.month?=?11
GROUP?BY?a.id,c.year,c.month)?a
LEFT?JOIN?(SELECT?a.id?as?payment_id,a.opener_id,a.money?as?payment_money,a.trode_number?from?sh_opener_bonus_payment?a
where?a.year?=?2015?and?a.`month`?=?11?and?a.agent_id?=?3)?b
on?a.opener_id?=?b.opener_id;
這里面有兩個子查詢語句,其實子查詢語句也是表,只不過是存在內存中罷了。
thinkphp實現:$useYear?=?date('Y',strtotime('last?month'));
$this->assign('useYear',$useYear);
$useMonth?=?date('m',strtotime('last?month'));
$this->assign('useMonth',$useMonth);
//?獲取上一月人員的獎金金額
//?子查詢1
$whereSub1['a.agent_id']?=?$this->agent_id;
$whereSub1['a.status']?=?1;
$whereSub1['c.year']?=?$useYear;
$whereSub1['c.month']?=?$useMonth;
$subQuery1?=?M()->table('sh_opener?a')->join('sh_opener_bonus?b?on?a.id?=?b.opener_id')->join('sh_incentive?c?on?b.incentive_id?=?c.id')->where($whereSub1)->group('a.id,c.year,c.month')->field('a.id,a.name,sum(c.money)?as?bonus_money,c.year,c.month')->select(false);
//?子查詢2
$whereSub2['a.agent_id']?=?$this->agent_id;
$whereSub2['a.year']?=?$useYear;
$whereSub2['a.month']?=?$useMonth;
$subQuery2?=?M()->table('sh_opener_bonus_payment?a')->where($whereSub2)->field('a.id?as?payment_id,a.opener_id,a.money?as?payment_money,a.trode_number')->select(false);
$list?=?M()->table($subQuery1.'?a')->join($subQuery2.'?b?on?a.id?=?b.opener_id')->select();
$this->assign('list',$list);
關于使用thinkPHP怎么實現一個子查詢語句就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
總結
以上是生活随笔為你收集整理的think php 子查询,使用thinkPHP怎么实现一个子查询语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言平面向量加法考点,平面向量的加减法
- 下一篇: java设计模式 外观,精掌握Java设