下面由Laravel教程栏目带大家介绍关于Laravel joinSub的使用,希望对大家有所帮助!
我有个语句,想着以为 join
子查询不支持改成模型查询的写法呢,百度了下,发现有 joinSub
的语法,在下孤陋寡闻了,特此记录
以下语句目的是,取全表最新日期的那条(相同字段中有重复时,只取最新)
表字段大写不是我定的~我只是查询使用者
$resultIds = DB::connection('fund')->select(" SELECT t1.InvestAdvisorCode FROM table t1 INNER JOIN ( SELECT SUBSTRING_INDEX( group_concat( id ORDER BY EndDate DESC ), ',', 1 ) AS id FROM table t2 GROUP BY InvestAdvisorCode ) t2 ON t1.id = t2.id order by t1.TotalFundNV desc ");
$subQuery = Table::query() ->selectRaw("SUBSTRING_INDEX( group_concat( id ORDER BY EndDate DESC ), ',', 1 ) AS id") ->from('table as t2') ->groupBy('InvestAdvisorCode') ->getQuery(); $resultIds=Table::query() ->from('table as t1') ->joinSub($subQuery,'t2','t1.id','=','t2.id') ->orderBy('t1.TotalFundNV','desc') ->pluck('InvestAdvisorCode')->toArray()