thinkPHP簡單實(shí)現(xiàn)多個(gè)子查詢語句的方法
本文實(shí)例講述了thinkPHP簡單實(shí)現(xiàn)多個(gè)子查詢語句的方法。分享給大家供大家參考,具體如下:
sql語句博大精深
理解好sql語句,就能用好thinkphp等框架中的數(shù)據(jù)庫操作
原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;
這里面有兩個(gè)子查詢語句,其實(shí)子查詢語句也是表,只不過是存在內(nèi)存中罷了。
thinkphp實(shí)現(xiàn):
$useYear = date('Y',strtotime('last month')); $this->assign('useYear',$useYear); $useMonth = date('m',strtotime('last month')); $this->assign('useMonth',$useMonth); // 獲取上一月人員的獎(jiǎng)金金額 // 子查詢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);
其實(shí)thinkphp框架對sql的封裝,最終還是要拼湊成sql語句。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- ThinkPHP采用GET方式獲取中文參數(shù)查詢無結(jié)果的解決方法
- ThinkPHP多表聯(lián)合查詢的常用方法
- thinkphp實(shí)現(xiàn)like模糊查詢實(shí)例
- ThinkPHP查詢語句與關(guān)聯(lián)查詢用法實(shí)例
- ThinkPHP視圖查詢詳解
- ThinkPHP查詢返回簡單字段數(shù)組的方法
- thinkphp學(xué)習(xí)筆記之多表查詢
- ThinkPHP中的常用查詢語言匯總
- ThinkPHP5查詢數(shù)據(jù)及處理結(jié)果的方法小結(jié)
- Thinkphp使用mongodb數(shù)據(jù)庫實(shí)現(xiàn)多條件查詢方法
- thinkPHP5框架閉包函數(shù)與子查詢傳參用法示例
相關(guān)文章
Zend Framework過濾器Zend_Filter用法詳解
這篇文章主要介紹了Zend Framework過濾器Zend_Filter用法,結(jié)合實(shí)例形式分析了過濾器Zend_Filter的功能、原理、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12關(guān)于DISCUZ不用通行證登陸得內(nèi)容介紹
DISCUZ是中國最常用的論壇,雖然他本身有通行證給大家連接,但實(shí)際上用戶的統(tǒng)一還是很不好,經(jīng)常要建立兩個(gè)用戶表,第一不利于注冊和管理,第二浪費(fèi)數(shù)據(jù)庫。2008-10-10PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(七)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀基礎(chǔ)數(shù)值部分,需要的朋友可以參考下2014-06-06PHP 中TP5 Request 請求對象的實(shí)例詳解
這篇文章主要介紹了PHP 中TP5 Request 請求對象的實(shí)例詳解的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-07-07