thinkPHP簡(jiǎn)單實(shí)現(xiàn)多個(gè)子查詢語(yǔ)句的方法
本文實(shí)例講述了thinkPHP簡(jiǎn)單實(shí)現(xiàn)多個(gè)子查詢語(yǔ)句的方法。分享給大家供大家參考,具體如下:
sql語(yǔ)句博大精深
理解好sql語(yǔ)句,就能用好thinkphp等框架中的數(shù)據(jù)庫(kù)操作
原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è)子查詢語(yǔ)句,其實(shí)子查詢語(yǔ)句也是表,只不過(guò)是存在內(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框架對(duì)sql的封裝,最終還是要拼湊成sql語(yǔ)句。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- ThinkPHP采用GET方式獲取中文參數(shù)查詢無(wú)結(jié)果的解決方法
- ThinkPHP多表聯(lián)合查詢的常用方法
- thinkphp實(shí)現(xiàn)like模糊查詢實(shí)例
- ThinkPHP查詢語(yǔ)句與關(guān)聯(lián)查詢用法實(shí)例
- ThinkPHP視圖查詢?cè)斀?/a>
- ThinkPHP查詢返回簡(jiǎn)單字段數(shù)組的方法
- thinkphp學(xué)習(xí)筆記之多表查詢
- ThinkPHP中的常用查詢語(yǔ)言匯總
- ThinkPHP5查詢數(shù)據(jù)及處理結(jié)果的方法小結(jié)
- Thinkphp使用mongodb數(shù)據(jù)庫(kù)實(shí)現(xiàn)多條件查詢方法
- thinkPHP5框架閉包函數(shù)與子查詢傳參用法示例
相關(guān)文章
php設(shè)計(jì)模式之抽象工廠模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之抽象工廠模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP抽象工廠模式的具體原理、使用技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
PHP讀取mssql json數(shù)據(jù)中文亂碼的解決辦法
PHP及網(wǎng)頁(yè)使用UTF-8編碼,數(shù)據(jù)庫(kù)是sql server2008,使用默認(rèn)編碼,當(dāng)讀取數(shù)據(jù)庫(kù)數(shù)據(jù)時(shí),使用php自帶的json_encode()返回到前端,結(jié)果中文不顯示。下面腳本之家小編給大家介紹PHP讀取mssql json數(shù)據(jù)中文亂碼的解決辦法,需要的朋友一起學(xué)習(xí)2016-04-04
Laravel 框架控制器 Controller原理與用法實(shí)例分析
這篇文章主要介紹了Laravel 框架控制器 Controller原理與用法,結(jié)合實(shí)例形式分析了Laravel 控制器 Controller基本概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
ThinkPHP框架任意代碼執(zhí)行漏洞的利用及其修復(fù)方法
這篇文章主要介紹了ThinkPHP框架任意代碼執(zhí)行漏洞的利用及其修復(fù)方法,該漏洞的修復(fù)對(duì)于廣大使用ThinkPHP的開發(fā)人員來(lái)說(shuō)尤為重要!需要的朋友可以參考下2014-07-07
CodeIgniter輔助之第三方類庫(kù)third_party用法分析
這篇文章主要介紹了CodeIgniter輔助之第三方類庫(kù)third_party用法,以CI集成Twig模版為例分析了CodeIgniter集成第三方類庫(kù)的實(shí)現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-01-01

