php - 在 Laravel 中使用 Eloquent 创建 count() 和 groupBy
问题描述
我一直在尝试创建这个 SQL 命令:
SELECT d.id, v.vote, count(*) as cnt FROM draws d, votes v WHERE v.draw_id = d.id AND v.vote = "pos" GROUP BY d.id ORDER BY cnt desc
我做的:
$draws = DB::table('draws')
->join('votes','draws.id', '=', 'votes.draw_id')
->select(DB::raw('draws.id, votes.vote, count(*) as cnt'))
->where('votes.vote', '=', 'pos')
->groupBy('draws.id')
->orderBy('cnt','DESC')
->get();
我不知道失败在哪里,有人可以帮助我吗?
错误:
SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #2 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列“cendradraw.votes.vote”;这与 sql_mode=only_full_group_by 不兼容(SQL:select draws.id, votes.vote, count(*) as cnt from draws
inner join votes
on draws
. id
= votes
. draw_id
where votes
. vote
= pos group by draws
. id
order by cnt
desc)
解决方案
推荐阅读
- python - If 语句检查 number 是否具有预期的位数
- c - 逐行读取文件到结构
- java - 有没有办法将文件中存在的元素存储到java中对象的数组中
- javascript - 如何将文件添加到 serializeArray
- reactjs - 删除包含用户数据的帐户
- graphql - SSR 和 GraphQL:如果不存在 javascript(SEO 目的),如何避免加载状态?
- macos - 在 Mac 上使用 VS 2019 生成的 docker compose 文件时出现问题
- c# - Blazor 输入掩码
- python - 未找到 Kivy 的模块
- python - 使用列表在python中创建数字网格