日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

think php 子查询,使用thinkPHP怎么实现一个子查询语句

發布時間:2025/3/15 php 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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怎么实现一个子查询语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。