SQL microsoft查询到Laravel 4.2

时间:2016-03-10 11:38:31

标签: php sql-server laravel

您好,全部来自所有国家/地区。 我需要将SQL查询转换为 LARAVEL 4.2 因为我还没有管理它的经验,我自己!

SQL位于 SQL server microsoft 2008

@foreach($data as $list) <tr> <td> {{ $list->id }} <td> {{ $list->fname}} <td> {{ $list->lname }} <td> {{ $list->contact }}; <td> <a href="{{url('/delete',$list->id)}}">Delete</a> @endforeach

我能做的只是在下一张图片enter image description here但是 $ id $ class_id 不是 在$ query中可识别....也是代码:

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

提前感谢您,只是为了寻找!

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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 $ class_id 。 $ id。''。$ class_id。'

我希望这对您有所帮助,因此对于复杂的查询...只需使用 RAW QUERIES