SELECT 列表不在 GROUP BY 子句中且包含非聚合列;这与 sql_mode=only_full_group_by 不兼容

SELECT list is not in GROUP BY clause and contains nonaggregated column; this is incompatible with sql_mode=only_full_group_by

这是我使用 Hibernate 的查询:

Query query = session.createQuery("
              From Invite 
             where (mobileNo= :email OR mobileNo= :phone) AND status= :status GROUP BY job");

我收到错误消息:

        SELECT list is not in 
      GROUP BY clause and contains nonaggregated column 'irecruter.invitecand0_.invite_id' which is not functionally dependent on columns in GROUP BY clause; this is  incompatible with sql_mode=only_full_group_by

我从 Whosebug 得到了解决方案:SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

但是错误又回来了。我想要永久的解决方案。有人可以对此提出建议吗?

尝试在查询末尾添加 invite_id 使其显示 GROUP BY job,invite_id"); 您需要分组依据 select 中的所有内容,所以这就是我现在的假设

整行:

Query query = session.createQuery("from Invite where (mobileNo= :email OR mobileNo= :phone) AND status= :status GROUP BY job,invite_id");

想象一个 table 存储论坛-post 的地方。您存储 post 本身、post 的日期以及 post 编辑它的用户。

当您执行以下操作时:

SELECT
    User_ID,
    PostedDate,
    COUNT(*) AS Posts
FROM myForum
WHERE PostedDate = CURDATE()
GROUP BY User_ID;

我们都知道你想要什么。数据库会说 "User_ID? yeah. It must be unique after the group by. Posts are aggregated by the group by but what the hell should I do with PostedDate? which one should I return?!?"。在这种情况下,它将是唯一的,但如果 where 子句不检查确切的 "PostedDate" 而是检查范围,则该语句未定义应返回哪个值。

安全模式下的这个设置可以防止有人犯这样的逻辑错误。如果您知道自己在做什么,则可以禁用检查或执行 "MIN"、"GROUP_CONCAT(DISTINCT PostedDate) AS PostedDate"、...

之类的操作

在这种情况下,您还可以使用 ANY_VALUE(),它会在该组中选择一个值。像这样使用它:

SELECT
    ANY_VALUE(myUsers.UserName) AS UserName,
    User_ID,
    COUNT(ForumPosts.Post) AS Posts
FROM myUsers
NATURAL JOIN ForumPosts
WHERE ForumPosts.PostDate = CURDATE()
GROUP BY User_ID;