Laravel查询构建器,有问题

时间:2017-03-21 19:12:39

标签: mysql database laravel having laravel-query-builder

我有一个问题,不知道如何解决它。 我需要从select中获取特定对象,例如 { “异”: “UA”, “故事”:122, “头衔”:乌克兰 }

所以我有一个SQL查询

SELECT
c.iso,
locale.title as title,
(SELECT COUNT(DISTINCT s.id) FROM stories AS s WHERE s.country_id = c.id) AS stories
FROM `countries` AS c
LEFT JOIN countries_locale AS c_l ON c.id=c_l.country_id
LEFT JOIN locales AS locale ON c_l.locale_id=locale.id
WHERE locale.locale = 'en'
GROUP BY c.id
HAVING stories>0

它工作正常,所以我尝试将此查询重写为Laravel QB:

DB::table($this->getTable())
->select(
'countries.iso as iso',
'locales.title as title',
DB::raw('(SELECT COUNT(s.id) FROM stories AS s WHERE  s.country_id = countries.id) AS stories')
)
->leftJoin('countries_locale', 'countries.id', '=', 'countries_locale.country_id')
->leftJoin('locales', 'countries_locale.locale_id', '=', 'locales.id')
->where('locales.locale', \App::getLocale())
->groupBy('iso')
->having('stories', '>', 0)
->get();

然后我收到错误

  

语法错误或访问冲突:1055'way.countries.id'不在GROUP BY

并向我展示了一个sql字符串,我可以在mysql中成功执行

1 个答案:

答案 0 :(得分:1)

如果您希望laravel接受非常严格的查询,请使用

'strict' => false

在数据库配置中。 或者,在您的情况下,您可以将这两列放在组中的选择中。