Laravel5.4查询在SQLServer上正常运行,但不是由LaravelElequent查询生成器运行我做错了什么?

jvlzgdj9  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(366)

我用的是Laravel5.4。我的书面质询与拉威尔雄辩。当我手动将查询写入mysql服务器时,它工作正常,但当我使用laravelelelounk编写相同的查询代码时,它就不工作了。
请帮忙。。。
拉斐尔码

$dateS = new Carbon($req->fromDate);
$dateE = new Carbon($req->toDate);

$tasks = DB::table('taskday')->selectRaw("taskday.dayID, taskday.task_date, taskday.fk_userID, userdetails.fullname, count(assignedtask.fk_dayID) as total")
            ->join("assignedtask","assignedtask.fk_dayID","taskday.dayID")
            ->join("userdetails","userdetails.userID","taskday.fk_userID")
            ->whereBetween("taskday.task_date", [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"]);
if($req->staffname > 0){
    $tasks = $tasks->where('taskday.fk_userID',$req->staffname);
}

$tasks = $tasks->groupBy("assignedtask.fk_dayID")->get();

return $tasks;

我的sql查询

select `taskday`.`dayID`, `taskday`.`task_date`, `taskday`.`fk_userID`, `userdetails`.`fullname`, count(`assignedtask`.`fk_dayID`) as total 
from `taskday` inner join `assignedtask` on `assignedtask`.`fk_dayID` = `taskday`.`dayID` 
inner join `userdetails` on `userdetails`.`userID` = `taskday`.`fk_userID` 
where `taskday`.`task_date` between '2018-11-01 00:00:00' and '2018-12-19 23:59:59' 
group by `assignedtask`.`fk_dayID`

直接在mysql上进行fire查询后,它会给出正确的结果

dayID    task_date   fk_userID   fullname    total
-------- ----------  ----------  ----------  -------------
DAY021   2018-12-02  USR300920   Manish      2
DAY022   2018-12-03  USR300924   Rovio       3

拉威尔给出了错误

(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1055 'medical.taskday.dayID'
 isn't in GROUP BY 
(SQL: select taskday.dayID, taskday.task_date, taskday.fk_userID, userdetails.fullname, count(assignedtask.fk_dayID) as total 
from `taskday` inner join `assignedtask` on `assignedtask`.`fk_dayID` = `taskday`.`dayID` 
inner join `userdetails` on `userdetails`.`userID` = `taskday`.`fk_userID` 
where `taskday`.`task_date` 
between 2018-11-01 00:00:00 and 2018-12-19 23:59:59 
group by `assignedtask`.`fk_dayID`)

请帮忙

irtuqstp

irtuqstp1#

如果我理解你的问题,试试这个。
控制器

$dateS = new Carbon($req->fromDate);
$dateE = new Carbon($req->toDate);

$tasks = DB::table('taskday')
    ->where('taskday.task_date')
    ->selectRaw("taskday.dayID, taskday.task_date, taskday.fk_userID, userdetails.fullname, count(assignedtask.fk_dayID) as total")
    ->join('assignedtask.fk_dayID', 'assignedtask', '=', 'taskday.dayID')
    ->join('userdetails.userID', 'userdetails', '=', 'taskday.fk_userID')
    ->whereBetween('taskday.task_date', [date('Y-m-d 00:00:00', strtotime($dateS)), date('Y-m-d 23:59:59', strtotime($dateE))])
    ->groupBy('assignedtask.fk_dayID')
    ->get();

if($req->staffname > 0){
    $tasks = $tasks->where('taskday.fk_userID',$req->staffname);
}

$tasks = $tasks->groupBy("assignedtask.fk_dayID")->get();

return $tasks;

请检查您的时间戳格式的数据库,它必须与此相同或尝试使用unixtime格式查询“wherebetween”更准确的计算。
看法

. . .
   <tr>
     <th>dayID</th>
     <th>task_date</th>
     <th>fk_userID</th>
     <th>fullname</th>
     <th>total</t>
   </tr>
@foreach($tasks as $task)
   </tr>
     <td>$task->dayID</td>
     <td>$task->task_date</td>
     <td>$task->fk_userID</td>
     <td>$task->fullname</td>
     <td>$task->total</td>
   </tr>
@endforeach
. . .

为了编码的和平:)

相关问题