简体   繁体   English

如何在mysql查询中解决“不在GROUP BY中”错误

[英]How to resolve “isn't in GROUP BY” error in mysql query

I have two models: posts and likings which have one-to-many relationship (so, one post has many likes). 我有两个模型: postslikings有一对多的关系(所以,一个帖子有很多喜欢)。 Likings model has also an isActive field which shows liking is active or passive. Likings模型还有一个isActive字段,表示喜欢是主动还是被动。

I want to get (sort) top 5 posts which had received maximum "active" likes (only likes whose isActive field is true would be considered). 我想获得(排序)前5个帖子,这些帖子已经收到了最大的“活跃”喜欢(只有喜欢其isActive字段为true的帖子才会被考虑)。

This is the query: 这是查询:

select posts.*, count(likings.id) as likes_count from 'posts'
left join 'likings' on 'likings'.'post_id' = 'posts'.'id' and 'likings'.'isActive' = 1
group by 'posts'.'id'
order by 'likes_count' desc 
limit 5

which is resulted from this laravel query: 这是由这个laravel查询产生的:

Post::selectRaw('posts.*, count(likes.id) as likes_count')
    ->leftJoin('likes', function ($join) {
        $join->on('likes.post_id', '=', 'posts.id')
             ->where('likes.is_active', '=', 1);
    })
    ->groupBy('posts.id')
    ->orderBy('likes_count', 'desc')
    ->take(5)
    ->get();

And this is the error: 这是错误:

SQLSTATE[42000]: Syntax error or access violation: 1055
'database.posts.user_id' isn't in GROUP BY

Here, posts.user_id is also a field of posts table and shows the owner of the post. 这里, posts.user_id也是posts表的一个字段,并显示posts的所有者。

How can I resolve this error? 我该如何解决这个错误? It seems it isn't logical to change mysql config (probably deleting ONLY_FULL_GROUP_BY mode), but minimum change in the query. 似乎改变mysql配置(可能删除ONLY_FULL_GROUP_BY模式)是ONLY_FULL_GROUP_BY ,但查询中的变化最小。

Each non-aggregated field should be grouped. 应对每个非聚合字段进行分组。

It is standard behavior, which in mysql, depends on ONLY_FULL_GROUP_BY mode. 它是标准行为,在mysql中,取决于ONLY_FULL_GROUP_BY模式。
This mode is default enabled in >= 5.7 . 在> = 5.7默认启用此模式。

select posts.id, post.user_id, count(likings.id) as likes_count
from posts
left join likings on likings.post_id = posts.id and likings.isActive= 1
group by posts.id, posts.user_id
order by likes_count desc 
limit 5

Or, you can aggregate it: 或者,您可以aggregate它:

select posts.id, MIN(post.user_id), count(likings.id) as likes_count
from posts
left join likings on likings.post_id = posts.id and likings.isActive= 1
group by posts.id
order by likes_count desc 
limit 5

Other way - change sql_mode : 其他方式 - 更改sql_mode

SET SESSION sql_mode = '';

Also, you can divide it to 2 query: 此外,您可以将其分为2个查询:

  1. Get aggregate and post ids 获取聚合和后期ID
  2. Get posts data with fetched ids (with IN clause ) 使用获取的id获取帖子数据(使用IN clause

In 5.7 the sqlmode is set by default to: 在5.7中,sqlmode默认设置为:

ONLY_FULL_GROUP_BY,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION ONLY_FULL_GROUP_BY,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION

To remove the clause ONLY_FULL_GROUP_BY you can do this: 要删除ONLY_FULL_GROUP_BY子句,您可以执行以下操作:

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM