【发布时间】:2019-09-12 00:47:38
【问题描述】:
我有以下疑问:
SELECT cn.id, cn.title, cn.type, cn.status FROM (
SELECT v.concert_id as concert_id, SUM(v.position_votes + v.support_votes) as votes
FROM (
SELECT q.concert_id as concert_id, q.position_id as position_id, q.position_votes as position_votes, SUM(q.votes_up + q.votes_down) as support_votes
FROM (
SELECT qcs.id AS concert_id, p.id AS position_id, p.votes AS position_votes, IF(s.votes_in_favor <=> null, 0, s.votes_in_favor) AS votes_up, IF(s.votes_not_in_favor <=> null, 0, s.votes_not_in_favor) AS votes_down
FROM (
SELECT concert_id AS id
FROM positions p
WHERE p.content LIKE '%%'
GROUP BY concert_id
)
AS qcs
JOIN positions p
ON p.concert_id = qcs.id
LEFT JOIN supports s
ON s.position_id = p.id
) AS q
GROUP BY q.position_id
) as v
GROUP BY v.concert_id
) as r
JOIN concerts cn on cn.id = r.concert_id
ORDER BY r.votes DESC, cn.created_at DESC
当我直接在 MySQL 中进行查询时,我得到了想要的结果。但是当我使用 DB 进行查询时,例如:
$query = "...alll_the_previous_query";
$result = DB::select(DB::raw($query));
我收到以下错误:
local.ERROR: SQLSTATE[42000]: Syntax error or access violation: 1055 'q.concert_id' isn't in GROUP BY ...the rest of the query
我知道避免这种情况的一种方法是更改 Laravel 中的数据库配置并将 strict 更改为 false。
但这不是一个选择。
当我将查询传递给 Laravel 时,我的查询出了什么问题?
【问题讨论】:
-
Laravel 默认使用严格模式。您应该根据错误消息修复您的查询。
-
是的,但是我尝试了很多,我不知道到底是什么错误。
-
而当你直接运行查询时,那么mysql不会使用严格模式。配置mysql使用严格sql模式,直接运行查询也会报错。
-
@JacoboTapia mysql 在这种特殊情况下的错误消息会告诉您确切的错误是什么。但是,修复此错误消息可能很容易,但之后您会收到不同的错误消息。我们对您的任务了解不足,无法知道如何修复查询。
-
什么是 MySQL 版本?
positions.id是主键吗?SUM(q.votes_up + q.votes_down) as support_votes是正确的还是您的意思是q.votes_up - q.votes_down?
标签: mysql laravel laravel-5 eloquent