Home > PHP Framework > Laravel > body text

Is there actually a joinSub syntax in Laravel?

藏色散人
Release: 2021-10-21 15:36:09
forward
2209 people have browsed it

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
");
Copy after login
      $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()
Copy after login
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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template