2016-03-10 74 views
0

你好,全部來自所有國家。 我需要一個SQL查詢轉換爲LARAVEL 4.2因爲我還沒有經驗來管理它,我自己!SQL微軟查詢到Laravel 4.2

的SQL是SQL服務器微軟2008

SELECT TOP(10) logst_data, LogSt_date, LogSt_studid FROM Home_LogStudents WHERE logst_action = 225 AND LogSt_sectionID = 257 AND LogSt_date IN (SELECT MIN(LogSt_date) FROM Home_LogStudents WHERE logst_data <> '0' AND logst_action = 225 AND LogSt_sectionID = 257 AND (LogSt_studid IN (select home_id FROM Hw_StudentClasses INNER JOIN Home_Students ON home_studid = Stclass_studid WHERE Stclass_classid = (129))) GROUP BY LogSt_studid) ORDER BY LogSt_data DESC

所有我能做的就是在接下來的圖片enter image description here$ ID$類標識碼不 可在$查詢中識別....也是代碼:

$users = DB::Table('Home_LogStudents') 
      ->select('LogSt_data','LogSt_date','LogSt_studid') 
      ->where('LogSt_action','=',225) 
      ->where('LogSt_sectionID','=',$id) 
      ->whereIn('LogSt_date',function($query){ 
       $query->select('LogSt_date') 
         ->from('Home_LogStudents') 
         ->where('LogSt_data','!=',0) 
         ->where('LogSt_action','=',225) 
         ->where('LogSt_sectionID','=',$id) 
         ->whereIn('LogSt_studid',function($query){ 
          $query->select('home_id') 
            ->from('Hw_StudentClasses') 
            ->join('Home_Students','Home_Students.home_studid','=','Hw_StudentClasses.Stclass_studid') 
            ->where('Stclass_classid','=',$class_id); 
          } 
         ) 
       ->groupBy('LogSt_studid'); 
       }) 
     ->orderBy('LogSt_data','DESC') 
     ->take(10) 
     ->get(); 

謝謝您提前,只爲尋找!

+0

我想你應該在這裏發佈的不是照片 – hungneox

+0

你的代碼,但基本上你需要使用'($類標識碼)',因爲匿名函數不能識別外部範圍變量。 – hungneox

+0

你的意思是用$查詢把$ id放在第一個子查詢中,在第二個子查詢中放$ class_id? –

回答

0

所以,我找到了答案,很容易使我所做的只是使用原始查詢導致查詢太複雜,要翻譯成laravel也許我不能做到這一點,所以我做了這個.. ...

$users = DB::select('SELECT  TOP (10) Home_LogStudents.LogSt_data, Home_LogStudents.LogSt_date, Home_LogStudents.LogSt_studid, Home_Students.home_firstname, Home_Students.home_lastname 
FROM   Home_LogStudents 
INNER JOIN Home_Students 
ON Home_LogStudents.LogSt_studid = Home_Students.home_id 
WHERE  (Home_LogStudents.LogSt_action = 225) 
AND (Home_LogStudents.LogSt_sectionID = '.$id.') 
AND (Home_LogStudents.LogSt_date IN 
          (SELECT  MIN(LogSt_date) AS Expr1 
          FROM   Home_LogStudents AS Home_LogStudents_1 
          WHERE  (LogSt_data <> \'0\') 
          AND (LogSt_action = 225) 
          AND (LogSt_sectionID = '.$id.') AND (LogSt_studid IN 
                 (SELECT  Home_Students_1.home_id 
                 FROM   Hw_StudentClasses INNER JOIN 
                       Home_Students AS Home_Students_1 
                 ON Home_Students_1.home_studid = Hw_StudentClasses.Stclass_studid 
                 WHERE  (Hw_StudentClasses.Stclass_classid = ' .$class_id.'))) 
          GROUP BY LogSt_studid)) 
ORDER BY Home_LogStudents.LogSt_data DESC 
'); 

其實我使用的字符串的$ ID之外,通過使用」的$類標識碼。 $ ID「。'。$ class_id。'

我希望這可以幫助你,所以對於複雜的查詢......只是使用RAW QUERIES

1

我沒有測試它,但您需要將參數$id, $class_id傳遞給匿名函數。

$users = DB::Table('Home_LogStudents') 
      ->select('LogSt_data','LogSt_date','LogSt_studid') 
      ->where('LogSt_action','=', 225) 
      ->where('LogSt_sectionID','=', $id) 
      ->whereIn('LogSt_date', function($query) use($id, $class_id) { 
       return $query->select('LogSt_date') 
        ->from('Home_LogStudents') 
        ->where('LogSt_data','!=', 0) 
        ->where('LogSt_action','=', 225) 
        ->where('LogSt_sectionID','=', $id) 
        ->whereIn('LogSt_studid', function($query) use($class_id){ 
         return $query->select('home_id') 
          ->from('Hw_StudentClasses') 
          ->join('Home_Students','Home_Students.home_studid','=','Hw_StudentClasses.Stclass_studid') 
          ->where('Stclass_classid','=', $class_id); 
         } 
        ) 
       ->groupBy('LogSt_studid'); 
       }) 
     ->orderBy('LogSt_data','DESC') 
     ->take(10) 
     ->get(); 
+0

好吧,我做到了,但現在它說_Undefined屬性:stdClass :: $ home_firstname_我使用** home_firstname **從** Home_Students **表在我的_blade view_這是從兩個不同的表中獲取特定** $ id的測試結果的信息的點**特定**的測試** class_id ** = class :( –

+0

我不確定,但你需要var_dump單行結果,我猜結果是數組,而不是一個對象,所以你可以試試'$ row ['home_firstname']'而不是'$ row-> home_firstname'? – hungneox

+0

是否有類似simiral的命令**對於Laravel,SQL Server的「Select MIN」**所有我知道** take()**和** first(); ** –