2017-09-07 2 views
0

Ich versuche, eine SQL-Abfrage an Laravel übergeben, aber in dieser Abfrage sind die Tabellen mit Composite-Primärschlüssel verbunden und zusätzlich Unterabfragen zu diesen Tabellen, so weiß ich nicht wie sie esLaravel Unterabfrage auswählen, Tabellen mit Composite-Primärschlüssel

der sQL-Code wird wie unten

  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 
gezeigt

es hat viele Unterabfragen wie diese beiden hat, die ich hier setzen, gleiche Bedingungen in die Verbindungen und derselben where-Klausel, aber verschiedene Tabellen, jede Idee, wie zu tun das in Laravel?

Antwort

0

Gelöst: 3, Ich kann es nicht glauben, dass dies einfach war habe ich versucht, dies und es funktionierte. Vielen Dank für Ihre Hilfe und Entschuldigung für die Mühe. : $

  $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();