您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用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怎么實現一個子查詢語句就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。