Home  >  Article  >  PHP Framework  >  Is there actually a joinSub syntax in Laravel?

Is there actually a joinSub syntax in Laravel?

藏色散人
藏色散人forward
2021-10-21 15:36:092199browse

The following tutorial column of Laravel will introduce you to the use of Laravel joinSub. I hope it will be helpful to everyone!

Is there actually a joinSub syntax in Laravel?

I have a statement, and I thought that the join subquery does not support the writing method of changing it to a model query. After searching on Baidu, I found that there is a syntax of joinSub, which is below I am ignorant, hereby record

The purpose of the following statement is to get the latest date in the entire table (if there are duplicates in the same field, only the latest one is taken)
The capitalization of the table fields is not determined by me~I am just querying User

        $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()
Related recommendations:The latest five Laravel video tutorials

The above is the detailed content of Is there actually a joinSub syntax in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete