【问题标题】:laravel subquery select, tables with composite primary keylaravel 子查询选择,具有复合主键的表
【发布时间】:2018-02-16 14:35:38
【问题描述】:

我正在尝试将 sql 查询传递给 laravel,但在此查询中,表是使用复合主键连接的,此外它还有这些表的子查询,所以我不知道该怎么做

sql代码如下图

            select distinct g.id,
            (   
                select count(*) from xxx s(nolock) inner join yyy t(nolock) 
                on t.id=s.id 
                and t.year=s.year 
                and t.code=s.code 
                where s.year = 2017 
                and t.status<>'C'
                and s.created_at>=convert(datetime,'16/08/2017 00:00:00',103) 
                and s.created_at<=convert(datetime,'22/08/2017 23:59:59',103) 
                and s.id=g.id
            ) as xxx,
            (
                select count(*) from zzz s(nolock) inner join yyy t(nolock) 
                on t.id=s.id 
                and t.year=s.year 
                and t.code=s.code 
                where s.year = 2017 
                and t.status<>'C'
                and s.created_at>=convert(datetime,'16/08/2017 00:00:00',103) 
                and s.created_at<=convert(datetime,'22/08/2017 23:59:59',103) 
                and s.id=g.id
            ) as zzz
            from globals g(nolock) order by g.id

它有很多像我放在这里的这两个子查询,连接中的相同条件和相同的 where 子句但不同的表,知道如何在 laravel 中执行此操作吗?

【问题讨论】:

    标签: sql laravel subquery


    【解决方案1】:

    可以在 Laravel 中使用原始查询。

    DB::select('select * from users where active = ?', [1]);

    在此处阅读有关此内容的更多信息 https://laravel.com/docs/5.4/database#running-queries

    【讨论】:

      【解决方案2】:

      已解决:3,我不敢相信它这么容易,我试过了,它奏效了。感谢您的帮助,很抱歉打扰。 :$

                  $data = \DB::table("globals")
                              ->select("globals.id",
                                  \DB::raw("(select count(*) from xxx s(nolock) inner join yyy t(nolock) 
                                          on t.id=s.id 
                                          and t.year=s.year 
                                          and t.code=s.code 
                                          where s.year = 2017 
                                          and t.status<>'C'
                                          and s.created_at>=convert(datetime,'16/08/2017 00:00:00',103) 
                                          and s.created_at<=convert(datetime,'22/08/2017 23:59:59',103) 
                                          and s.id=globals.id
                                      ) as xxx
                                  "),
                                  \DB::raw("(select count(*) from zzz s(nolock) inner join yyy t(nolock) 
                                          on t.id=s.id 
                                          and t.year=s.year 
                                          and t.code=s.code 
                                          where s.year = 2017 
                                          and t.status<>'C'
                                          and s.created_at>=convert(datetime,'16/08/2017 00:00:00',103) 
                                          and s.created_at<=convert(datetime,'22/08/2017 23:59:59',103) 
                                          and s.id=globals.id
                                      ) as zzz
                                  ")
                              )
                              ->orderBy('globals.id', 'asc')
                              ->get();
      

      【讨论】:

        猜你喜欢
        • 2014-07-31
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 2013-09-04
        • 1970-01-01
        • 2011-07-03
        • 2016-03-25
        • 1970-01-01
        相关资源
        最近更新 更多