国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

thinkPHP簡單實現多個子查詢語句的方法

2024-05-04 23:53:16
字體:
來源:轉載
供稿:網友

本文實例講述了thinkPHP簡單實現多個子查詢語句的方法。分享給大家供大家參考,具體如下:

sql語句博大精深

理解好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 aLEFT JOIN sh_opener_bonus b on a.id = b.opener_idLEFT JOIN sh_incentive c on b.incentive_id = c.idwhere a.agent_id = 3 and a.status = 1 and c.year = 2015 and c.month = 11GROUP BY a.id,c.year,c.month) aLEFT JOIN (SELECT a.id as payment_id,a.opener_id,a.money as payment_money,a.trode_number from sh_opener_bonus_payment awhere a.year = 2015 and a.`month` = 11 and a.agent_id = 3) bon 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框架對sql的封裝,最終還是要拼湊成sql語句。

希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 盐亭县| 文山县| 泽库县| 元朗区| 南澳县| 横山县| 东宁县| 浦东新区| 湟源县| 天镇县| 木里| 靖边县| 固镇县| 永吉县| 班玛县| 垦利县| 石家庄市| 定边县| 南和县| 广汉市| 瓦房店市| 西乡县| 沂南县| 蒙自县| 大渡口区| 隆昌县| 罗田县| 芒康县| 裕民县| 盐津县| 蕉岭县| 和林格尔县| 赤城县| 宁强县| 庆元县| 泰安市| 马关县| 吉首市| 宜川县| 东乌珠穆沁旗| 吉木萨尔县|