douhe2305 2016-11-02 10:45
浏览 152

Laravel Query构建器 - 查询不起作用但在SQL控制台中工作

I've become extremely confused I'm trying to run a query through laravels query builder which should work but it's throwing odd errors.

I'm trying to count records from the tips table where the status is 'Won' between a daterange and then join the users table on the user_id to retrieve the users username.

This is my query builder

    $topfiveTipsters = DB::table('tips')
      ->select(DB::raw('count(status) as wincount, users.name'))
      ->join('users', 'users.id', '=', 'tips.user_id')
      ->whereBetween('tips.created_at',[$start,$end])
      ->where('status','Won')
      ->groupBy('users.id')
      ->orderBy('wincount', 'desc')
      ->get();

However it's throwing the error

QLSTATE[42000]: Syntax error or access violation: 1055 'digthetip.users.name' isn't in GROUP BY (SQL: select count(status) as wincount, users.name from tips inner join users on users.id = tips.user_id where tips.created_at between 2016-11-01 00:00:00 and 2016-11-02 10:39:02 and status = Won group by users.id order by wincount desc)

but if I run the exact query the error has outputted into the SQL console within PHPMyAdmin the query runs fine and returns the results I require.

Am I missing something? I'm relatively new to Laravel and I'm massively confused how it's still throwing an error.

  • 写回答

1条回答 默认 最新

  • drcj64241 2016-11-02 11:01
    关注

    Ok so it turns out you can turn of SQL strict mode in /app/database.php which will prevent the error from happening.

    'strict' => false,

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序